Repaso
Español
TestingCoreTST-26

What happens when an assert statement in a pytest test is false?

def test_total():
    assert calculate_total([10, 20]) == 30

Options

  1. Nothing happens and the test continues to run
  2. The test fails, and pytest shows the compared values (Correct option)
  3. Python crashes with a traceback and stops all tests
  4. The test is skipped and flagged in the report

Answer

The test fails, and pytest shows the values that were compared

Why

assert condition raises an AssertionError when the condition is false. pytest catches it, marks the test as FAILED and shows a detailed message, e.g. assert 25 == 30, so you see what the code actually returned.

If the condition is true, nothing happens and the test passes.

Remember

assert false → test fails with the compared values shown.

Key terms

Practice this question in Repaso