0
0
PyTesttesting~10 mins

Checking exception attributes in PyTest - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check that the exception message contains the word 'error'.

PyTest
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]
Drag options to blanks, or click blank then click option'
Aargs
Btext
Cmessage
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'message' attribute which does not exist on the exception object.
Trying to access exc_info.message directly.
2fill in blank
medium

Complete the code to assert that the exception's first argument equals 'Invalid input'.

PyTest
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'
Drag options to blanks, or click blank then click option'
A-1
B1
C0
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 1 which is out of range for a single-argument exception.
Using negative index incorrectly.
3fill in blank
hard

Fix the error in the code to correctly check the exception message contains 'timeout'.

PyTest
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]
Drag options to blanks, or click blank then click option'
Aargs
Btext
Cmsg
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent attributes like 'message' or 'msg'.
Trying to access exc_info.message directly.
4fill in blank
hard

Fill both blanks to check the exception type and that its message equals 'File not found'.

PyTest
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'
Drag options to blanks, or click blank then click option'
AFileNotFoundError
BValueError
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong exception type in pytest.raises.
Using index 1 instead of 0 for the message.
5fill in blank
hard

Fill all three blanks to assert the exception type, check the message contains 'denied', and verify the error code attribute equals 403.

PyTest
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
Drag options to blanks, or click blank then click option'
APermissionErrorWithCode
B0
Ccode
DPermissionError
Attempts:
3 left
💡 Hint
Common Mistakes
Using the base PermissionError instead of the custom subclass.
Checking the message at wrong index.
Trying to access a non-existent attribute instead of 'code'.