assertRaises-type tests should only contain a single statement
JelleZijlstra opened this issue · comments
Consider this code:
with pytest.raises(TypeError):
1 + "x"
"x" + 1
This will pass if only one of the two statements raises, which is probably not what the author wanted. Instead, each statement should be in its own pytest.raises
context. (Real-world example: python/cpython#103880.)
We should show an error if an assertRaises-type context (pytest.raises
, self.assertRaises
, self.assertRaisesRegex
) contains more than one top-level statement.
Yup - sure, This is definitely a bug that could hide failures. PR would be accepted.
I'd like to keep the scope to testing here tho to start with. This could open a can of worms if we look at context mangers/exception handling more broadly ...
Can be closed @cooperlees