0
0
PyTesttesting~20 mins

Checking exception attributes in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exception Attribute Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this pytest exception attribute check?
Consider the following pytest test code. What will be the value of exc_info.value.args[0] after the test runs?
PyTest
import pytest

def test_divide_by_zero():
    with pytest.raises(ZeroDivisionError) as exc_info:
        result = 1 / 0
    assert exc_info.value.args[0] == 'division by zero'
    print(exc_info.value.args[0])
A"division by zero"
B"ZeroDivisionError"
C"float division by zero"
DIndexError
Attempts:
2 left
💡 Hint
Check the message stored in the exception's args attribute.
assertion
intermediate
2:00remaining
Which assertion correctly checks the exception message in pytest?
You want to verify that a ValueError is raised with the message 'invalid input'. Which assertion line is correct inside a pytest test using pytest.raises?
PyTest
import pytest

def test_invalid_input():
    with pytest.raises(ValueError) as exc_info:
        raise ValueError('invalid input')
    # Which assertion below is correct?
Aassert exc_info.value.args == ('invalid input',)
Bassert exc_info.value.message == 'invalid input'
Cassert str(exc_info.value) == 'invalid input'
Dassert exc_info.message == 'invalid input'
Attempts:
2 left
💡 Hint
Use the string representation of the exception to check the message.
🔧 Debug
advanced
2:00remaining
Why does this pytest exception attribute check fail?
This test is supposed to check the exception message but fails. What is the cause?
PyTest
import pytest

def test_key_error():
    with pytest.raises(KeyError) as exc_info:
        raise KeyError('missing_key')
    assert exc_info.value.args[0] == 'missing_key'
    assert str(exc_info.value) == 'missing_key'

# The test fails on the second assertion.
AThe args attribute is empty, so args[0] raises IndexError
BThe string representation of KeyError includes quotes, so str(exc_info.value) is "'missing_key'" not "missing_key"
CKeyError does not have args attribute
DThe exception was not raised, so exc_info is None
Attempts:
2 left
💡 Hint
Print the string form of the exception to see its exact content.
🧠 Conceptual
advanced
1:30remaining
What attribute of the pytest exception info object holds the caught exception instance?
When using with pytest.raises(SomeError) as exc_info:, which attribute of exc_info contains the actual exception instance?
Aexc_info.value
Bexc_info.type
Cexc_info.message
Dexc_info.args
Attempts:
2 left
💡 Hint
The exception instance is stored in an attribute named 'value'.
framework
expert
3:00remaining
How to correctly check multiple exception attributes in pytest?
You want to test that a function raises a RuntimeError with message 'fail' and has a custom attribute code equal to 500. Which pytest test code is correct?
PyTest
import pytest

class CustomError(RuntimeError):
    def __init__(self, message, code):
        super().__init__(message)
        self.code = code

def func():
    raise CustomError('fail', 500)

# Which test below is correct?
A
def test_func():
    with pytest.raises(RuntimeError) as exc_info:
        func()
    assert exc_info.value.args[0] == 'fail'
    assert exc_info.code == 500
B
def test_func():
    with pytest.raises(CustomError) as exc_info:
        func()
    assert exc_info.value.args == ('fail', 500)
    assert exc_info.value.code == 500
C
def test_func():
    with pytest.raises(CustomError) as exc_info:
        func()
    assert exc_info.message == 'fail'
    assert exc_info.value.code == 500
D
def test_func():
    with pytest.raises(CustomError) as exc_info:
        func()
    assert str(exc_info.value) == 'fail'
    assert exc_info.value.code == 500
Attempts:
2 left
💡 Hint
Check the exception instance's string and custom attribute separately.