I noticed that pytest is overriding custom assertion statements unless they're called directly in a test body or the statement is explicitly assert False
. Here is some example code showing the interaction:
def assert_equality():
assert 1 == 0, 'pytest will not display this message'
def assert_boolean():
assert False, 'this message will be displayed'
def test_nested_assertion_equality():
assert_equality()
def test_nested_assertion_boolean():
assert_boolean()
def test_assertion_equality():
assert 1 == 0, 'this message also will be displayed'
Output for these tests:
test_experiment.py:8 (test_nested_assertion_equality)
1 != 0
Expected :0
Actual :1
<Click to see difference>
def test_nested_assertion_equality():
> assert_equality()
test_experiment.py:10:
test_experiment.py:12 (test_nested_assertion_boolean)
def test_nested_assertion_boolean():
> assert_boolean()
test_experiment.py:14:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
def assert_boolean():
> assert False, 'this message will be displayed'
E AssertionError: this message will be displayed
E assert False
test_experiment.py:6: AssertionError
test_experiment.py:16 (test_assertion_equality)
1 != 0
Expected :0
Actual :1
<Click to see difference>
def test_assertion_equality():
> assert 1 == 0, 'this message also will be displayed'
E AssertionError: this message also will be displayed
E assert 1 == 0
test_experiment.py:18: AssertionError
Why is this happening? Is there a way to stop it?
Pytest does override the asserts, it's called assert rewriting and is documented here: https://docs.pytest.org/en/7.1.x/how-to/assert.html
You can do a lot to modify its behavior and disable it by adding the command line argument --assert=plain
.