Using pytest.raises
or unittest.TestCase.assertRaises
will assert that an exception is raised in the following block.
Ending such block in an assertion means that the test can succeed with that last assertion never being executed.
Noncompliant code example
import pytest
def foo(): return 1 / 0
def bar(): return 42
def test_something():
with pytest.raises(ZeroDivisionError):
foo()
assert bar() == 42 # Noncompliant
Compliant solution
import pytest
def foo(): return 1 / 0
def bar(): return 42
def test_something():
with pytest.raises(ZeroDivisionError):
foo()
assert bar() == 42