Complete the code to check that the exception message contains the word 'error'.
import pytest def test_error_message(): with pytest.raises(ValueError) as exc_info: raise ValueError('This is an error message') assert 'error' in exc_info.value.[1][0]
The args attribute of an exception contains the message as a tuple. Checking if 'error' is in exc_info.value.args verifies the message.
Complete the code to assert that the exception's first argument equals 'Invalid input'.
import pytest def test_invalid_input(): with pytest.raises(ValueError) as exc_info: raise ValueError('Invalid input') assert exc_info.value.args[[1]] == 'Invalid input'
The first argument of the exception message is at index 0 in the args tuple.
Fix the error in the code to correctly check the exception message contains 'timeout'.
import pytest def test_timeout_error(): with pytest.raises(TimeoutError) as exc_info: raise TimeoutError('Connection timeout') assert 'timeout' in exc_info.value.[1][0]
The exception message is stored in args, not in message or other attributes.
Fill both blanks to check the exception type and that its message equals 'File not found'.
import pytest def test_file_error(): with pytest.raises([1]) as exc_info: raise FileNotFoundError('File not found') assert exc_info.value.args[[2]] == 'File not found'
The exception type to catch is FileNotFoundError. The message is the first argument at index 0.
Fill all three blanks to assert the exception type, check the message contains 'denied', and verify the error code attribute equals 403.
import pytest class PermissionErrorWithCode(PermissionError): def __init__(self, message, code): super().__init__(message) self.code = code def test_permission_error(): with pytest.raises([1]) as exc_info: raise PermissionErrorWithCode('Access denied', 403) assert 'denied' in exc_info.value.args[[2]] assert exc_info.value.[3] == 403
The test catches the custom exception PermissionErrorWithCode. The message is the first argument at index 0. The custom attribute code is checked for the error code.