How to assert that a function call does not return an error with unittest?

2024/10/8 22:15:12

Is there anyway with unittest to just assert that a function call does not result in an error, whether it is a TypeError, IOError, etc.

example:

assert function(a,b) is not error

or

if not assertRaises function(a, b)

What is the best way to do this (without using classes)? The trouble I'm having is all the useful documentation I come a cross in unittest is all class based and I don't want to use class based.

Full example:

def test_validate_params():assert test.validate_params('hackenv-re', 'test.username') is not errorsassert test.validate_params('fakeenv', 'test.username') is errorassert test.validate_params('hackevn-re', 'vagrant') is errorcounter += 1print "Test Passed {0}/5: validate_params".format(counter)
Answer

If your function raises an exception, the test will fail. There is no need to do any additional checks. If you absolutely want to, you can do something like this:

try:function_under_test()
except Exception as e:self.fail("Unexpected exception %s" % e) 

(assuming standard Python unittest)

https://en.xdnf.cn/q/118665.html

Related Q&A

How to calculate Python float-number-th root of float number

I found the following answer here on Stackoverflow:https://stackoverflow.com/a/356187/1829329But it only works for integers as n in nth root:import gmpy2 as gmpyresult = gmpy.root((1/0.213), 31.5).real…

Need help making a Hilbert Curve using numbers in Python

I want to make a function that will create a Hilbert Curve in python using numbers. The parameters for the function would be a number and that will tell the function how many times it should repeat. To…

How do I separate each list [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Why python doesnt see the members of quantumCircuit class qiskit

I`m trying to learn the programming on quantum computers. I have installed qiskit in VS Code (all qiskit extentions available in VS Code market) , python compilator (from Vs Code market "Python&qu…

Compare multiple lines in a text file using python

Im writing a program that is time tracking report that reports the time that was spent in each virtual machine, I`m having a problem comparing the txt file cause the only thing that changes are the num…

Error installing eomaps through conda and pip

I am getting the following error output when trying to install eomaps using Conda. I dont know how to solve this. I have also tried the same using pip but it didnt seem to solve the problem. Here is th…

DFS on a graph using a python generator

I am using a generator to do a full search on a graph, the real data set is fairly large, here is a portion of the code i wrote on a small data set:class dfs:def __init__(self):self.start_nodes = [1,2]…

Importing test libraries failed. No module named a

I have a folder /example which contains /Libs which further contains different folders /a, /b each containing python libraries. I am trying to run a robot framework code from /example.The error it show…

Sorting characters by count using PHP or Python

I have a string of charactersabcdefghijklmnopqrstuvwxyz_ I want to take this string of characters and sort them by the number of times they appear in a large block of characters. For example: cwrxwzb…

Save user first_name as default value for model django

I have an article model with author variable which I want to save as the users first and last name. I use custom user model called Account. author = models.CharField(author,max_length=50 default=User.f…