0
0
PyTesttesting~20 mins

Testing exception chains in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exception Chain Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of chained exception test with pytest
What is the output of this pytest test when run?
PyTest
import pytest

def func():
    try:
        raise ValueError("Initial error")
    except ValueError as e:
        raise RuntimeError("Chained error") from e

def test_exception_chain():
    with pytest.raises(RuntimeError) as excinfo:
        func()
    assert isinstance(excinfo.value.__cause__, ValueError)
    assert str(excinfo.value) == "Chained error"
ATest fails because excinfo.value.__cause__ is None
BTest fails because str(excinfo.value) is 'Initial error'
CTest passes with no assertion errors
DTest raises an unexpected TypeError during execution
Attempts:
2 left
💡 Hint
Remember that 'raise ... from ...' sets the __cause__ attribute of the exception.
assertion
intermediate
1:30remaining
Correct assertion to verify exception cause in pytest
Which assertion correctly verifies that a caught exception has a cause of type KeyError?
PyTest
import pytest

with pytest.raises(RuntimeError) as excinfo:
    raise RuntimeError("Error") from KeyError("missing key")
Aassert isinstance(excinfo.value.__cause__, KeyError)
Bassert excinfo.value.__cause__ == KeyError
Cassert excinfo.value.__context__ is None
Dassert excinfo.value.__cause__.args == ('Error',)
Attempts:
2 left
💡 Hint
The __cause__ attribute holds the original exception instance, not the class.
🔧 Debug
advanced
2:00remaining
Why does this pytest test fail to detect the cause exception?
Given this test code, why does the assertion fail?
PyTest
import pytest

def func():
    try:
        raise IndexError("Index problem")
    except IndexError:
        raise RuntimeError("Runtime problem")

def test_chain():
    with pytest.raises(RuntimeError) as excinfo:
        func()
    assert excinfo.value.__cause__ is not None
ABecause the RuntimeError was raised without 'from', so __cause__ is None
BBecause the test should check __context__ instead of __cause__
CBecause the original exception was swallowed and replaced, causing a TypeError
DBecause pytest.raises does not capture __cause__ attribute
Attempts:
2 left
💡 Hint
Check how exception chaining works when 'from' is omitted.
🧠 Conceptual
advanced
1:30remaining
Understanding exception chaining attributes
Which statement about Python exception chaining is TRUE?
AUsing 'raise' without 'from' clears both __cause__ and __context__
B__context__ is always None when an exception is raised inside except block
C__cause__ and __context__ always point to the same exception instance
D__cause__ is set when an exception is raised with 'from', otherwise None
Attempts:
2 left
💡 Hint
Think about how explicit and implicit chaining differ.
framework
expert
2:30remaining
Testing nested exception chains with pytest
Consider this nested exception chain. Which pytest assertion correctly verifies the root cause exception type?
PyTest
import pytest

def nested_func():
    try:
        raise KeyError("key missing")
    except KeyError as e:
        raise ValueError("value error") from e

def test_nested_chain():
    with pytest.raises(ValueError) as excinfo:
        nested_func()
    # Which assertion below correctly checks the root cause is KeyError?
Aassert isinstance(excinfo.value.__cause__.__cause__, KeyError)
Bassert isinstance(excinfo.value.__cause__, KeyError)
Cassert excinfo.value.__context__.__cause__ is None
Dassert excinfo.value.__cause__.args[0] == 'value error'
Attempts:
2 left
💡 Hint
Check the chain depth and which exception is caught by pytest.raises.