Complete the code to check that a ValueError is raised.
import pytest def test_value_error(): with pytest.raises([1]): int('abc')
The int('abc') call raises a ValueError because 'abc' cannot be converted to an integer.
Complete the code to check that a KeyError is raised with the correct message.
import pytest def test_key_error(): with pytest.raises(KeyError) as exc_info: {}['missing'] assert exc_info.value.args[0] == [1]
The KeyError message is the missing key as a string. It is stored in exc_info.value.args[0].
Fix the error in the test to correctly check the exception chain.
import pytest def test_exception_chain(): with pytest.raises(RuntimeError) as exc_info: try: int('abc') except ValueError as e: raise RuntimeError('Failed') from [1]
The from keyword requires the original exception instance, which is e here.
Fill both blanks to assert the original exception type and message in the exception chain.
import pytest def test_exception_chain_details(): with pytest.raises(RuntimeError) as exc_info: try: int('abc') except ValueError as e: raise RuntimeError('Failed') from e original = exc_info.value.__cause__ assert isinstance(original, [1]) assert str(original) == [2]
The original exception is a ValueError with the message about invalid literal.
Fill all three blanks to write a test that verifies the exception chain and message.
import pytest def test_full_exception_chain(): with pytest.raises([1]) as exc_info: try: float('xyz') except [2] as e: raise [3]('Conversion failed') from e
The test expects a RuntimeError raised from a caught ValueError when converting 'xyz' to float.