[issue32213] assertRaises and subTest context managers cannot be nested

R. David Murray report at bugs.python.org
Mon Dec 4 11:08:15 EST 2017


R. David Murray <rdmurray at bitdance.com> added the comment:

To be pedantic, are not macros, they are context managers :)

Your first case is not something I would have thought of coding.  In the to_raise=True case, the subTest is failing because an exception is raised inside its scope.  In the to_raise=False case, the subTest scope has already ended before the assertRaises scope completes and raises its error because no exception was raised.  That is, there is no subTest in effect to be reported when that failure occurs.

In your second case, when to_raise is False, no exception is raised, so the assertRaises correctly fails, and the subtest reports that to_raise is False.  When to_raise is true, the exception is raised, the assertRaises passes and so does the subtest.  In other words, I can't reproduce the problem you cite for the second case.  Looking at what you pasted, it looks like you confused a test_assert_outer report with a test_report_inner report.

So, as far as I can see, there's nothing broken here, everything is working according to the documentation :)

----------
nosy: +r.david.murray

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32213>
_______________________________________


More information about the Python-bugs-list mailing list