BrunoLaget BrunoLaget - 10 months ago 49
Python Question

python unittest - assert any raise

I have some codes structured in this way:

if "ok": return True
else: return False

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.


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

assertRaises(Exception, Method)