Friday, August 23, 2013

Python: Using assertRaises as a Context Manager

If you're using the unittest library, and you want to check the value of an exception, here's a convenient way to use assertRaises:

with self.assertRaises(ValueError) as context_manager:
    call_some_function()
self.assertIn("Oops, something went wrong", str(context_manager.exception))

No comments: