0
0
PyTesttesting~20 mins

pytest.raises context manager - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
pytest.raises Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of pytest.raises with ZeroDivisionError
What will be the output of this test code when run with pytest?
PyTest
import pytest

def test_division():
    with pytest.raises(ZeroDivisionError):
        x = 1 / 0
    print("Test passed")
ANo output because test is skipped
BZeroDivisionError is raised and test fails
CSyntaxError due to incorrect pytest usage
DTest passed
Attempts:
2 left
💡 Hint
pytest.raises expects the code inside to raise the specified error to pass.
assertion
intermediate
2:00remaining
Correct assertion to check exception message with pytest.raises
Which option correctly asserts that a ValueError with message 'invalid input' is raised?
PyTest
import pytest

def func():
    raise ValueError('invalid input')

def test_func():
    with pytest.raises(ValueError) as excinfo:
        func()
    # Which assertion below is correct?
Aassert excinfo.value.message == 'invalid input'
Bassert excinfo.message == 'invalid input'
Cassert 'invalid input' in str(excinfo.value)
Dassert excinfo == 'invalid input'
Attempts:
2 left
💡 Hint
Exception message is accessed via str(excinfo.value).
🔧 Debug
advanced
2:00remaining
Why does this pytest.raises test fail?
Given this test code, why does pytest report a failure?
PyTest
import pytest

def test_error():
    with pytest.raises(ValueError):
        x = int('abc')
    with pytest.raises(TypeError):
        x = int('abc')
AThe first with block does not raise any error
BThe second with block expects TypeError but int('abc') raises ValueError
Cpytest.raises cannot be used twice in one test
Dint('abc') raises SyntaxError, not ValueError or TypeError
Attempts:
2 left
💡 Hint
Check the actual error type raised by int('abc').
🧠 Conceptual
advanced
2:00remaining
Behavior of pytest.raises when no exception is raised
What happens if the code inside a pytest.raises context manager does NOT raise the expected exception?
AThe test fails with Failed: DID NOT RAISE <ExceptionType>
BThe test passes silently
Cpytest.raises raises a SyntaxError
DThe test is skipped automatically
Attempts:
2 left
💡 Hint
pytest.raises expects an exception to be raised inside the block.
framework
expert
3:00remaining
Using pytest.raises to test multiple exception types
Which option correctly tests that a function raises either ValueError or TypeError using pytest.raises?
A
with pytest.raises((ValueError, TypeError)):
    func()
B
with pytest.raises(ValueError | TypeError):
    func()
C
with pytest.raises(ValueError) and pytest.raises(TypeError):
    func()
D
with pytest.raises(ValueError) or pytest.raises(TypeError):
    func()
Attempts:
2 left
💡 Hint
pytest.raises accepts a tuple of exception types to check multiple exceptions.