BrunoLaget BrunoLaget - 1 year ago 118
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.

Answer Source

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

assertRaises(Exception, Method)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download