0
0
PyTesttesting~20 mins

Testing multiple exceptions in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple Exceptions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
ATest passes but does not catch any exceptions.
BTest passes with both exceptions correctly caught.
CTest fails because only the first exception is caught; second raises an error.
DTest fails due to a SyntaxError in the test code.
Attempts:
2 left
💡 Hint
pytest.raises can be used multiple times in one test function to check different exceptions.
assertion
intermediate
2: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
A
with pytest.raises(ValueError):
    func(0)
with pytest.raises(TypeError):
    func(1)
B
with pytest.raises((ValueError, TypeError)):
    func(0)
    func(1)
C
with pytest.raises(ValueError) or pytest.raises(TypeError):
    func(0)
    func(1)
Dassert func(0) raises ValueError and func(1) raises TypeError
Attempts:
2 left
💡 Hint
Use separate pytest.raises blocks for different exceptions.
🔧 Debug
advanced
2: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)
ABecause the test function is missing a return statement.
BBecause pytest.raises cannot catch multiple exceptions in one block.
CBecause func(1) does not raise any exception.
DBecause func(0) raises ValueError and exits the with block before func(1) is called.
Attempts:
2 left
💡 Hint
Consider how exceptions interrupt code flow inside a with block.
framework
advanced
2: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
A
def test_func():
    with pytest.raises(ValueError):
        func(0)
    with pytest.raises(TypeError):
        func(1)
B
def test_func():
    with pytest.raises((ValueError, TypeError)):
        func(0)
        func(1)
C
def test_func():
    assert func(0) raises ValueError
    assert func(1) raises TypeError
D
def test_func():
    try:
        func(0)
    except ValueError:
        pass
    try:
        func(1)
    except TypeError:
        pass
Attempts:
2 left
💡 Hint
Use separate with blocks for each exception test.
🧠 Conceptual
expert
2: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?
ABecause combining exceptions in one block causes syntax errors.
BBecause pytest.raises only supports one exception type per test file.
CBecause each pytest.raises block stops execution on exception, so separate blocks ensure all exceptions are tested.
DBecause pytest.raises automatically retries the block until all exceptions occur.
Attempts:
2 left
💡 Hint
Think about how exceptions affect code flow inside a with block.