Bird
Raised Fist0
PyTesttesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using pytest.warns in a test?
easy
A. To skip tests that produce warnings
B. To ignore all warnings during test runs
C. To raise an error when no warnings occur
D. To check if a specific warning is raised during code execution

Solution

  1. Step 1: Understand the role of pytest.warns

    pytest.warns is used to verify that a warning is raised when running a piece of code.
  2. Step 2: Compare options with this purpose

    Only To check if a specific warning is raised during code execution correctly states that pytest.warns checks for specific warnings during execution.
  3. Final Answer:

    To check if a specific warning is raised during code execution -> Option D
  4. Quick Check:

    pytest.warns checks warnings = A [OK]
Hint: Remember: pytest.warns confirms warnings appear [OK]
Common Mistakes:
  • Thinking pytest.warns ignores warnings
  • Confusing pytest.warns with error raising
  • Assuming pytest.warns skips tests
2. Which of the following is the correct syntax to assert a warning of type UserWarning using pytest?
easy
A. with pytest.warns(UserWarning):\n function_call()
B. pytest.warns(UserWarning, function_call())
C. assert pytest.warns(UserWarning, function_call)
D. pytest.warns(function_call(), UserWarning)

Solution

  1. Step 1: Recall pytest.warns usage

    pytest.warns is used as a context manager with with to catch warnings during code execution.
  2. Step 2: Check syntax correctness

    with pytest.warns(UserWarning):\n function_call() correctly uses with pytest.warns(UserWarning): followed by the code that triggers the warning.
  3. Final Answer:

    with pytest.warns(UserWarning):\n function_call() -> Option A
  4. Quick Check:

    Use 'with' for pytest.warns = B [OK]
Hint: Always use 'with' block for pytest.warns [OK]
Common Mistakes:
  • Calling pytest.warns as a function without 'with'
  • Passing function call instead of function inside 'with'
  • Swapping arguments order
3. What will be the result of this pytest code snippet?
import warnings
import pytest

def func():
    warnings.warn('Deprecated feature', DeprecationWarning)

def test_func():
    with pytest.warns(DeprecationWarning) as record:
        func()
    assert len(record) == 1
    assert record[0].message.args[0] == 'Deprecated feature'
medium
A. Test fails because no warning is raised
B. Test passes because the warning is correctly caught and message matches
C. Test fails due to wrong warning type
D. Test raises an exception because record is not iterable

Solution

  1. Step 1: Analyze the warning raised by func()

    The function func() raises a DeprecationWarning with message 'Deprecated feature'.
  2. Step 2: Check pytest.warns usage and assertions

    The test uses pytest.warns to catch DeprecationWarning and stores it in record. It asserts one warning was caught and the message matches exactly.
  3. Final Answer:

    Test passes because the warning is correctly caught and message matches -> Option B
  4. Quick Check:

    Warning caught and message correct = A [OK]
Hint: Check warning type and message inside pytest.warns block [OK]
Common Mistakes:
  • Assuming warnings.warn does not raise warnings
  • Confusing warning types
  • Not using 'as record' to capture warnings
4. Identify the error in this pytest warning assertion code:
import warnings
import pytest

def test_warning():
    with pytest.warns(UserWarning):
        warnings.warn('Be careful!')
    assert pytest.warns(UserWarning)
medium
A. The assert statement incorrectly calls pytest.warns as a function
B. The warning message is missing in pytest.warns
C. The warning type UserWarning is invalid
D. The warnings.warn call should be outside the with block

Solution

  1. Step 1: Review the use of pytest.warns

    pytest.warns is a context manager used with 'with' to catch warnings, not a function to assert directly.
  2. Step 2: Identify incorrect assert usage

    The assert line calls pytest.warns(UserWarning) as a function, which is invalid and causes an error.
  3. Final Answer:

    The assert statement incorrectly calls pytest.warns as a function -> Option A
  4. Quick Check:

    pytest.warns used only with 'with' block = C [OK]
Hint: Do not call pytest.warns as a function; use 'with' block only [OK]
Common Mistakes:
  • Calling pytest.warns without 'with'
  • Expecting pytest.warns to return a boolean
  • Placing warnings.warn outside the 'with' block
5. You want to test a function that may raise either DeprecationWarning or UserWarning. How can you assert that exactly one warning of either type is raised during the test?
hard
A. Use two separate with blocks for each warning type and assert one warning each
B. Use a single with block with pytest.warns(Warning) and check the warning type inside
C. Use pytest.warns with a tuple of warning types and assert one warning caught
D. Use pytest.warns without specifying warning type and count warnings

Solution

  1. Step 1: Understand pytest.warns supports multiple warning types

    pytest.warns can accept a tuple of warning classes to catch any of those types.
  2. Step 2: Use tuple to catch either DeprecationWarning or UserWarning

    Using pytest.warns((DeprecationWarning, UserWarning)) catches either warning type in one block.
  3. Step 3: Assert exactly one warning was caught

    Inside the block, assert that one warning was raised to ensure test precision.
  4. Final Answer:

    Use pytest.warns with a tuple of warning types and assert one warning caught -> Option C
  5. Quick Check:

    Tuple of warnings in pytest.warns = D [OK]
Hint: Pass tuple of warnings to pytest.warns to catch multiple types [OK]
Common Mistakes:
  • Using multiple with blocks causing multiple warnings
  • Not specifying warning types and catching unrelated warnings
  • Assuming pytest.warns accepts multiple calls in one block
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard