Test Overview
This test checks that a function raises a ValueError when given invalid input. It verifies that the exception is correctly thrown and caught using pytest.raises.
This test checks that a function raises a ValueError when given invalid input. It verifies that the exception is correctly thrown and caught using pytest.raises.
import pytest def divide(a, b): if b == 0: raise ValueError("Cannot divide by zero") return a / b def test_divide_by_zero(): with pytest.raises(ValueError) 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 is ready to execute test_divide_by_zero | - | PASS |
| 2 | Calls divide(10, 0) | Function divide is executing with a=10, b=0 | - | PASS |
| 3 | Function detects b=0 and raises ValueError("Cannot divide by zero") | Exception ValueError is raised inside divide function | - | PASS |
| 4 | pytest.raises context catches the ValueError exception | Exception is caught and stored in exc_info | Check that exception type is ValueError | PASS |
| 5 | Assert that exception message equals "Cannot divide by zero" | Exception message is accessible via exc_info.value | assert str(exc_info.value) == "Cannot divide by zero" | PASS |
| 6 | Test ends successfully | Test framework records test_divide_by_zero as passed | - | PASS |