Bird
0
0

Find the issue in this test code:

medium📝 Debug Q7 of 15
PyTest - Writing Assertions
Find the issue in this test code:
import pytest
import warnings

def test_warn():
    pytest.warns(UserWarning, warnings.warn('Warning!'))
AFunction call is executed before pytest.warns context
Bwarnings.warn requires a message and category
CUserWarning is not a valid warning type
Dpytest.warns requires a context manager, not a function call
Step-by-Step Solution
Solution:
  1. Step 1: Understand pytest.warns usage

    pytest.warns is designed to be used as a context manager to catch warnings during code execution.
  2. Step 2: Identify misuse of pytest.warns

    The code calls warnings.warn inside pytest.warns as a function argument, which is incorrect usage.
  3. Final Answer:

    pytest.warns requires a context manager, not a function call -> Option D
  4. Quick Check:

    Use pytest.warns as context manager, not function call [OK]
Quick Trick: Use 'with pytest.warns():' not pytest.warns(func()) [OK]
Common Mistakes:
MISTAKES
  • Passing function call instead of using context manager
  • Confusing pytest.warns with pytest.raises
  • Not wrapping code inside 'with' block

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes