BrunoLaget BrunoLaget - 3 months ago 10
Python Question

python unittest - assert any raise

I have some codes structured in this way:

Method(args):
try:
{method}
if "ok": return True
else: return False
except:
raise


And I have at least 3 unit tests to perform on this method, one to assert an ideal True condition, at least one where I expect Method to return False, and I wish to build a test that returns "ok" when any exception/error is raised.

I know about assertRaise already, but it asks for a specific exception, and I wish to assert any condition raised as true.

Answer

Since you are essentially catching every exception type, your assertRaises should expect the most basic exception type, which is Exception.

assertRaises(Exception, Method)
Comments