Complete the code to check that a ValueError is raised.
import pytest def test_value_error(): with pytest.raises([1]): int('abc')
The pytest.raises() function expects the exception class to check. Here, int('abc') raises a ValueError.
Complete the code to check that the exception message contains 'invalid literal'.
import pytest def test_value_error_message(): with pytest.raises(ValueError, match=[1]): int('abc')
The match argument expects a string or regex pattern to match the exception message. The message for int('abc') contains 'invalid literal'.
Fix the error in the code to correctly match the exception message.
import pytest def test_zero_division(): with pytest.raises(ZeroDivisionError, match=[1]): 1 / 0
The exception message for dividing by zero is 'division by zero'. The match argument must match this exact substring.
Fill both blanks to check for a KeyError with message 'missing'.
import pytest def test_key_error(): with pytest.raises([1], match=[2]): d = {} d['missing']
The code raises a KeyError when accessing a missing dictionary key. The message is the key name, here 'missing'.
Fill all three blanks to check for an IndexError with message 'list index out of range'.
import pytest def test_index_error(): with pytest.raises([1], match=[2]): lst = [1, 2, 3] _ = lst[[3]]
Accessing index 5 in a list of length 3 raises IndexError with message 'list index out of range'.