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
This is the ideal situation for the
subTests
context manager. However, this is only available in python 3. I think the best solution would be to create your own version ofsubTests
. The benefits being it's easy to setup a basic mimicry ofsubTests
, and ifsubTests
is ever back ported to python 2 then it will be easy to switch.Which produces the following output: