I'm using assertRaises in a loop like this:
for i in ['a', 'b', 'c']:
self.assertRaises(ValidationError, my_method, i)
And the problem is that whenever test fails, the output looks like this:
File "/bla/test.py", line 4, in test_assertion
my_method(i)
AssertionError: ValidationError not raised
How to make it print out the value of i when test fails? Like this:
File "/bla/test.py", line 4, in test_assertion
my_method('b')
AssertionError: ValidationError not raised
One solution is to use
assertRaises()as a context manager.The print statement will only execute when the
my_method()line passes successfully, so it will appear as the output when your test fails. Bit of a hack, but it works (unlessmy_method()throws some error that is not aValidationError).