0
0
PyTesttesting~20 mins

Matching exception messages in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exception Matcher Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
assertion
intermediate
2:00remaining
Matching exception message with pytest.raises

Which pytest code snippet correctly asserts that a ValueError is raised with the exact message 'Invalid input' when calling func()?

PyTest
def func():
    raise ValueError('Invalid input')
A
with pytest.raises(ValueError) as excinfo:
    func()
assert 'Invalid input' in str(excinfo.value)
B
with pytest.raises(ValueError, message='Invalid input'):
    func()
C
with pytest.raises(ValueError, match='Invalid input'):
    func()
D
with pytest.raises(ValueError, match='invalid input'):
    func()
Attempts:
2 left
💡 Hint

Check the correct parameter name for matching exception messages in pytest.raises.

Predict Output
intermediate
2:00remaining
Output of pytest.raises with partial message match

What will happen when running this pytest test?

def func():
    raise RuntimeError('File not found error')

def test_func():
    with pytest.raises(RuntimeError, match='not found'):
        func()
PyTest
def func():
    raise RuntimeError('File not found error')

def test_func():
    with pytest.raises(RuntimeError, match='not found'):
        func()
ATest fails with AssertionError because exception type is wrong
BTest fails because match requires exact full message match
CTest raises a SyntaxError due to invalid match parameter
DTest passes because 'not found' is a substring of the exception message
Attempts:
2 left
💡 Hint

Remember that match uses regular expression search, so partial matches work.

🔧 Debug
advanced
2:00remaining
Why does this pytest.raises match fail?

Given this code, why does the test fail?

def func():
    raise KeyError('missing key')

def test_func():
    with pytest.raises(KeyError, match=r'^missing key$'):
        func()
PyTest
def func():
    raise KeyError('missing key')

def test_func():
    with pytest.raises(KeyError, match=r'^missing key$'):
        func()
AThe exception message includes quotes, so the match string does not match exactly
BThe exception type is wrong, should be ValueError
Cpytest.raises does not support match parameter for KeyError exceptions
DThe match parameter is case sensitive and should be 'Missing Key'
Attempts:
2 left
💡 Hint

Check how Python formats KeyError messages.

framework
advanced
2:00remaining
Best practice for matching exception messages in pytest

Which practice is best when matching exception messages in pytest tests?

AUse the <code>match</code> parameter with a simple substring or regex to avoid brittle tests
BAvoid matching messages and only check exception type to reduce test complexity
CMatch the full exact exception message string to ensure strict correctness
DUse <code>assert</code> statements inside <code>pytest.raises</code> block to check message manually
Attempts:
2 left
💡 Hint

Think about test maintenance and message variability.

🧠 Conceptual
expert
2:00remaining
Understanding pytest.raises match parameter behavior

What is the behavior of the match parameter in pytest.raises when matching exception messages?

AIt compares the exception message using exact string equality
BIt performs a regular expression search on the string representation of the exception message
CIt matches only the start of the exception message string
DIt matches the exception type name instead of the message
Attempts:
2 left
💡 Hint

Recall how match uses regex internally.