A. logging.error() is called before caplog is imported
B. caplog.at_level() must be used as a context manager with 'with'
C. assertion should check caplog.records instead of caplog.text
D. logging level ERROR is too high to capture
Solution
Step 1: Check usage of caplog.at_level()
caplog.at_level() is a context manager and must be used with 'with' keyword.
Step 2: Identify missing 'with' statement
The code calls caplog.at_level() without 'with', so it does not set the level properly.
Final Answer:
caplog.at_level() must be used as a context manager with 'with' -> Option B
Quick Check:
Use 'with caplog.at_level()' not just call [OK]
Hint: Always use 'with' for caplog.at_level() context [OK]
Common Mistakes:
Calling caplog.at_level() without 'with'
Checking wrong caplog attribute
Misunderstanding log level capture
5. You want to test that your function logs a WARNING message only when a certain condition is true. Which pytest test code correctly uses caplog to check this behavior?
hard
A. def test_warn_log(caplog):
with caplog.at_level(logging.WARNING):
my_function(True)
assert 'Warning triggered' in caplog.text
with caplog.at_level(logging.WARNING):
my_function(False)
assert 'Warning triggered' not in caplog.text
B. def test_warn_log(caplog):
with caplog.at_level(logging.INFO):
my_function(True)
assert 'Warning triggered' in caplog.text
C. def test_warn_log(caplog):
with caplog.at_level(logging.WARNING):
my_function(True)
my_function(False)
assert caplog.text.count('Warning triggered') == 1
D. def test_warn_log(caplog):
caplog.set_level(logging.WARNING)
my_function(True)
assert 'Warning triggered' in caplog.text
my_function(False)
assert 'Warning triggered' not in caplog.text
Solution
Step 1: Understand the requirement
We want to check that the warning message logs only once when condition is true, not when false.
Step 2: Analyze options for correct caplog usage
def test_warn_log(caplog):
with caplog.at_level(logging.WARNING):
my_function(True)
my_function(False)
assert caplog.text.count('Warning triggered') == 1 uses one context capturing both calls and asserts the warning appears exactly once, matching the requirement.
Step 3: Why other options fail
def test_warn_log(caplog):
with caplog.at_level(logging.WARNING):
my_function(True)
assert 'Warning triggered' in caplog.text
with caplog.at_level(logging.WARNING):
my_function(False)
assert 'Warning triggered' not in caplog.text: separate contexts but caplog.text accumulates logs, so second assert 'Warning triggered' not in caplog.text fails (message from first call persists); def test_warn_log(caplog):
caplog.set_level(logging.WARNING)
my_function(True)
assert 'Warning triggered' in caplog.text
my_function(False)
assert 'Warning triggered' not in caplog.text: caplog.set_level does not exist (AttributeError), and accumulation would fail second assert; def test_warn_log(caplog):
with caplog.at_level(logging.INFO):
my_function(True)
assert 'Warning triggered' in caplog.text: tests only True case with INFO level (captures WARNING but no False verification).
Final Answer:
def test_warn_log(caplog):
with caplog.at_level(logging.WARNING):
my_function(True)
my_function(False)
assert caplog.text.count('Warning triggered') == 1 -> Option C
Quick Check:
Use one caplog context and count messages [OK]
Hint: Use one with caplog.at_level() block and count messages [OK]
Common Mistakes:
Using multiple caplog contexts losing logs
Using wrong log level in at_level
Assuming caplog.set_level exists
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.