0
0
PyTesttesting~20 mins

Testing custom exceptions in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Exception Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of pytest test for custom exception
What is the test result when running this pytest code that checks for a custom exception?
PyTest
import pytest

class MyError(Exception):
    pass

def func():
    raise MyError("fail")

def test_func_raises():
    with pytest.raises(MyError):
        func()
ATest fails with MyError uncaught
BTest fails with AssertionError
CTest passes with no errors
DTest raises TypeError during execution
Attempts:
2 left
💡 Hint
pytest.raises expects the specified exception to be raised inside the with block.
assertion
intermediate
2:00remaining
Correct assertion to test exception message
Which assertion correctly tests that the exception message contains the word 'timeout'?
PyTest
import pytest

def func():
    raise TimeoutError("Connection timeout occurred")

def test_func():
    with pytest.raises(TimeoutError) as exc_info:
        func()
    # Which assertion below is correct here?
Aassert exc_info.value.args[0] == 'timeout'
Bassert 'timeout' in str(exc_info.value)
Cassert exc_info.value.message == 'timeout'
Dassert exc_info.message == 'timeout'
Attempts:
2 left
💡 Hint
Exception message is accessed by converting exc_info.value to string.
🔧 Debug
advanced
2:00remaining
Identify the error in this pytest exception test
Why does this pytest test fail with an error instead of passing?
PyTest
import pytest

class CustomError(Exception):
    pass

def func():
    raise CustomError("error")

def test_func():
    with pytest.raises(CustomError):
        func
Afunc is referenced without parentheses, so exception is never raised
Bpytest.raises expects a string, not an exception class
CCustomError is not derived from Exception
DThe exception message must be checked inside pytest.raises
Attempts:
2 left
💡 Hint
Check how func is called inside the with block.
🧠 Conceptual
advanced
2:00remaining
Why use custom exceptions in testing?
What is the main advantage of defining and testing custom exceptions in your code?
AThey make the code run faster by skipping error checks
BThey automatically fix bugs without extra code
CThey reduce the need for writing test cases
DThey allow more precise error handling and clearer test failure reasons
Attempts:
2 left
💡 Hint
Think about how custom exceptions help in identifying specific problems.
framework
expert
2:00remaining
Best pytest pattern for testing multiple custom exceptions
You want to test that a function raises either CustomErrorA or CustomErrorB depending on input. Which pytest pattern correctly tests this in one test function?
AUse pytest.raises with a tuple of exceptions: with pytest.raises((CustomErrorA, CustomErrorB)): func(x)
BWrite two separate test functions, each with pytest.raises for one exception
CUse try-except inside the test and assert exception type manually
DUse pytest.raises with a list of exceptions: with pytest.raises([CustomErrorA, CustomErrorB]): func(x)
Attempts:
2 left
💡 Hint
Check pytest documentation for multiple exceptions in raises.