Test Overview
This test checks that a function raises a ValueError with the exact expected message when given invalid input.
This test checks that a function raises a ValueError with the exact expected message when given invalid input.
import pytest def divide(a, b): if b == 0: raise ValueError("Cannot divide by zero") return a / b def test_divide_by_zero_message(): with pytest.raises(ValueError, match="Cannot divide by zero") as exc_info: divide(10, 0) assert str(exc_info.value) == "Cannot divide by zero"
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | pytest test runner initialized | - | PASS |
| 2 | Calls divide(10, 0) | Function divide is executing with a=10, b=0 | - | PASS |
| 3 | Function raises ValueError with message 'Cannot divide by zero' | Exception raised inside divide function | pytest checks exception type is ValueError | PASS |
| 4 | pytest matches exception message with 'Cannot divide by zero' | Exception message available for matching | Exception message matches exactly | PASS |
| 5 | Assertion checks exception message string equals 'Cannot divide by zero' | Exception info captured in exc_info | assert str(exc_info.value) == 'Cannot divide by zero' | PASS |
| 6 | Test ends successfully | All assertions passed | - | PASS |