Challenge - 5 Problems
Multiple Exceptions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of pytest test with multiple exception checks
What is the test result when running this pytest test function that checks for multiple exceptions?
PyTest
import pytest def func(x): if x == 0: raise ValueError("Zero not allowed") elif x == 1: raise TypeError("One is wrong type") return x def test_func_exceptions(): with pytest.raises(ValueError): func(0) with pytest.raises(TypeError): func(1)
Attempts:
2 left
💡 Hint
pytest.raises can be used multiple times in one test function to check different exceptions.
✗ Incorrect
The test calls func with 0 and 1 inside separate pytest.raises blocks, so both exceptions are caught and the test passes.
❓ assertion
intermediate2:00remaining
Correct assertion to check multiple exceptions in pytest
Which assertion correctly tests that a function raises either ValueError or TypeError when called with certain inputs?
PyTest
def func(x): if x == 0: raise ValueError("Zero not allowed") elif x == 1: raise TypeError("One is wrong type") return x
Attempts:
2 left
💡 Hint
Use separate pytest.raises blocks for different exceptions.
✗ Incorrect
Option A correctly uses separate pytest.raises blocks to check each exception individually. Option A incorrectly tries to catch both exceptions in one block but calls both functions inside it, which will fail on the first exception and not reach the second call.
🔧 Debug
advanced2:00remaining
Debug why second exception test is not reached
Why does the second exception test not run in this pytest code?
PyTest
import pytest def func(x): if x == 0: raise ValueError("Zero not allowed") elif x == 1: raise TypeError("One is wrong type") return x def test_func(): with pytest.raises((ValueError, TypeError)): func(0) func(1)
Attempts:
2 left
💡 Hint
Consider how exceptions interrupt code flow inside a with block.
✗ Incorrect
When func(0) raises ValueError, the with block exits immediately, so func(1) is never called. To test both exceptions, separate with blocks are needed.
❓ framework
advanced2:00remaining
Best pytest pattern to test multiple exceptions separately
Which pytest test function correctly tests that func raises ValueError for 0 and TypeError for 1, ensuring both exceptions are tested independently?
PyTest
def func(x): if x == 0: raise ValueError("Zero not allowed") elif x == 1: raise TypeError("One is wrong type") return x
Attempts:
2 left
💡 Hint
Use separate with blocks for each exception test.
✗ Incorrect
Option A uses separate pytest.raises blocks, which is the recommended way to test multiple exceptions independently. Option A fails to test the second call because the first exception interrupts the block. Option A is invalid syntax. Option A does not use pytest's assertion mechanism and will not fail the test if exceptions are not raised.
🧠 Conceptual
expert2:00remaining
Why separate pytest.raises blocks are preferred for multiple exceptions
Why is it better to use separate pytest.raises blocks when testing multiple exceptions in one test function?
Attempts:
2 left
💡 Hint
Think about how exceptions affect code flow inside a with block.
✗ Incorrect
Each pytest.raises block exits as soon as the expected exception is raised, so if multiple exceptions are tested in one block, only the first is caught. Separate blocks allow testing each exception independently.