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 |
pytest.raises in a test?pytest.raisespytest.raises is used to verify that a certain exception is raised by the code inside its with block.with block correctly states that it checks for a specific exception. Other options describe unrelated behaviors.with block -> Option DValueError is raised when calling func() using pytest.raises?pytest.raises syntaxwith statement with pytest.raises(ExceptionType): followed by the code that should raise the exception.with block and exception type. pytest.raises(ValueError, func()) calls the function inside the argument, which runs before checking. with pytest.raises('ValueError'): func() uses a string instead of the exception class. pytest.raises(ValueError): func() misses the with keyword.import pytest
def test_zero_division():
with pytest.raises(ZeroDivisionError) as exc_info:
1 / 0
assert 'division by zero' in str(exc_info.value)with pytest.raises(ZeroDivisionError) as exc_info: block raises a ZeroDivisionError by dividing 1 by 0.def test_key_error():
with pytest.raises(KeyError):
d = {'a': 1}
value = d['b']
assert value == 1value = d['b'] raises a KeyError, so value is never assigned inside the with block.with block, the code tries to assert value == 1, but value is undefined because the assignment never happened due to the exception.parse_int(s) that should raise ValueError with message containing 'invalid literal' when given a non-integer string. Which pytest code correctly asserts this behavior and checks the error message?with pytest.raises(ValueError) as e: to catch the exception and store info in e.str(e.value). This converts the exception to string for message checking.e outside the block without capturing it. pytest.raises(ValueError, parse_int('abc'))
assert 'invalid literal' in e.value calls the function inside pytest.raises incorrectly and uses e undefined. with pytest.raises('ValueError') as e:
parse_int('abc')
assert 'invalid literal' in str(e.value) uses exception name as string instead of class.Pass the test and this pattern moves unknown