0
0
PyTesttesting~20 mins

Asserting exceptions (pytest.raises) - Practice Problems & Coding Challenges

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

def test_divide_by_zero():
    with pytest.raises(ZeroDivisionError):
        x = 1 / 0
ATest passes because ZeroDivisionError is correctly caught
BTest fails because ZeroDivisionError is not caught
CTest raises a SyntaxError due to incorrect pytest usage
DTest fails because the exception type is wrong
Attempts:
2 left
💡 Hint
Remember pytest.raises expects the exact exception type to catch.
assertion
intermediate
2:00remaining
Detecting wrong exception type in pytest.raises
What happens when this test runs?
PyTest
import pytest

def test_value_error():
    with pytest.raises(KeyError):
        int('abc')
ATest fails because ValueError is raised, not KeyError
BTest passes because any exception is accepted
CTest raises a TypeError due to wrong exception type
DTest passes because KeyError is raised
Attempts:
2 left
💡 Hint
Check which exception int('abc') actually raises.
Predict Output
advanced
2:00remaining
Output of pytest.raises context manager with exception info
What will be the value of 'exc_type' after running this test?
PyTest
import pytest

def test_exception_info():
    with pytest.raises(ValueError) as exc_info:
        raise ValueError('Invalid input')
    exc_type = exc_info.type
    print(exc_type.__name__)
ANone
BKeyError
CValueError
DTypeError
Attempts:
2 left
💡 Hint
exc_info.type holds the exception class caught by pytest.raises.
🔧 Debug
advanced
2:00remaining
Why does this pytest.raises test fail unexpectedly?
Identify why this test fails even though it seems to catch the exception.
PyTest
import pytest

def test_key_error():
    with pytest.raises(KeyError):
        d = {'a': 1}
        value = d['b']
        print('Accessed value:', value)
ATest fails because print statement is outside the with block
BTest fails because KeyError is not raised inside the with block
CTest fails because print statement executes after exception and causes error
DTest passes because KeyError is raised inside the with block
Attempts:
2 left
💡 Hint
Check where the exception is raised and if the with block covers it.
framework
expert
3:00remaining
Using pytest.raises to assert exception message content
Which option correctly asserts that the exception message contains the word 'timeout'?
PyTest
import pytest

def test_timeout_error():
    with pytest.raises(RuntimeError) as exc_info:
        raise RuntimeError('Connection timeout occurred')
    # Assertion here
Aassert exc_info.value.message == 'timeout'
Bassert 'timeout' in str(exc_info.value)
Cassert exc_info.type == 'timeout'
Dassert exc_info.message.contains('timeout')
Attempts:
2 left
💡 Hint
Exception message is accessed via str(exc_info.value).