0
0
PyTesttesting~20 mins

Asserting warnings (pytest.warns) - Practice Problems & Coding Challenges

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

def test_warning_capture():
    with pytest.warns(DeprecationWarning) as record:
        warnings.warn("Deprecated feature", DeprecationWarning)
    return len(record)

result = test_warning_capture()
A1
B0
CRaises AssertionError
DRaises TypeError
Attempts:
2 left
💡 Hint
Count how many warnings are captured inside the context.
assertion
intermediate
2:00remaining
Correct assertion to check warning message
Which assertion correctly verifies that the warning message contains the word 'deprecated'?
PyTest
import warnings
import pytest

def test_warning_message():
    with pytest.warns(UserWarning) as record:
        warnings.warn("This feature is deprecated", UserWarning)
    warning_message = str(record[0].message)
    # Which assertion below is correct?
Aassert warning_message == 'deprecated'
Bassert 'deprecated' in warning_message
Cassert warning_message.startswith('deprecated')
Dassert warning_message is 'deprecated'
Attempts:
2 left
💡 Hint
Check if the substring is anywhere in the message.
🔧 Debug
advanced
2:00remaining
Identify the error in warning assertion
What error will this test raise when run?
PyTest
import warnings
import pytest

def test_wrong_warning():
    with pytest.warns(DeprecationWarning):
        warnings.warn("Use new API", UserWarning)
AAssertionError
BNo error, test passes
CTypeError
DRuntimeWarning
Attempts:
2 left
💡 Hint
Check if the warning type matches the expected one.
framework
advanced
2:00remaining
Using pytest.warns as a function decorator
Which option correctly uses pytest.warns as a decorator to check for a warning?
PyTest
import warnings
import pytest

# Choose the correct decorator usage
A
@pytest.warns(DeprecationWarning)
def test_func():
    pass
B
@pytest.mark.warns(DeprecationWarning)
def test_func():
    warnings.warn("Deprecated", DeprecationWarning)
C
@pytest.warns(UserWarning)
def test_func():
    warnings.warn("Deprecated", DeprecationWarning)
D
@pytest.warns(DeprecationWarning)
def test_func():
    warnings.warn("Deprecated", DeprecationWarning)
Attempts:
2 left
💡 Hint
Check the correct decorator name and matching warning type.
🧠 Conceptual
expert
2:00remaining
Behavior of pytest.warns with multiple warnings
If a code block inside pytest.warns(WarningType) emits multiple warnings, how does pytest.warns behave?
AIt only passes if exactly one warning of WarningType is raised
BIt fails if more than one warning is raised, even if all are WarningType
CIt passes if at least one warning of WarningType is raised, ignoring others
DIt raises an error if any warning other than WarningType is raised
Attempts:
2 left
💡 Hint
Think about how pytest.warns matches warnings inside the block.