Challenge - 5 Problems
Exception Attribute Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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])
Attempts:
2 left
💡 Hint
Check the message stored in the exception's args attribute.
✗ Incorrect
The
ZeroDivisionError exception's first argument contains the error message string 'division by zero'. Accessing exc_info.value.args[0] retrieves this message.❓ assertion
intermediate2: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?
Attempts:
2 left
💡 Hint
Use the string representation of the exception to check the message.
✗ Incorrect
The
message attribute does not exist on exception instances. The correct way is to convert the exception to string using str() and compare it to the expected message.🔧 Debug
advanced2: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.
Attempts:
2 left
💡 Hint
Print the string form of the exception to see its exact content.
✗ Incorrect
The string form of a KeyError includes quotes around the key, so
str(exc_info.value) returns "'missing_key'" with quotes. The assertion comparing to 'missing_key' without quotes fails.🧠 Conceptual
advanced1: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?Attempts:
2 left
💡 Hint
The exception instance is stored in an attribute named 'value'.
✗ Incorrect
The
exc_info object has a value attribute that holds the caught exception instance. Other attributes like type hold the exception class, not the instance.❓ framework
expert3: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?
Attempts:
2 left
💡 Hint
Check the exception instance's string and custom attribute separately.
✗ Incorrect
Option D correctly asserts the string message via
str(exc_info.value) and accesses the custom attribute code on the exception instance. Option D wrongly accesses exc_info.code which does not exist. Option D uses exc_info.message which is invalid. Option D wrongly expects args to contain both message and code.