Test Overview
This test checks if a custom exception is raised correctly when a function receives invalid input. It verifies that the exception type and message are as expected.
This test checks if a custom exception is raised correctly when a function receives invalid input. It verifies that the exception type and message are as expected.
import pytest class CustomError(Exception): pass def divide(a, b): if b == 0: raise CustomError("Cannot divide by zero") return a / b def test_divide_raises_custom_error(): with pytest.raises(CustomError) 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 | Raises CustomError with message 'Cannot divide by zero' | Exception raised inside divide function | pytest.raises context captures CustomError | PASS |
| 4 | Checks exception message equals 'Cannot divide by zero' | Exception message available in exc_info.value | assert str(exc_info.value) == 'Cannot divide by zero' | PASS |
| 5 | Test ends | Test passed successfully | - | PASS |