Challenge - 5 Problems
Log Level Filtering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this pytest log capture?
Given the following pytest test code with log level filtering, what will be captured in the test log output?
PyTest
import logging def test_logging(caplog): with caplog.at_level(logging.WARNING): logging.debug('Debug message') logging.warning('Warning message') logging.error('Error message') print(caplog.text)
Attempts:
2 left
💡 Hint
Remember that caplog.at_level sets the minimum log level to capture.
✗ Incorrect
caplog.at_level(logging.WARNING) captures logs with level WARNING and above. DEBUG is below WARNING, so it is not captured.
❓ assertion
intermediate2:00remaining
Which assertion correctly checks that only ERROR logs are captured?
In a pytest test using caplog with level ERROR, which assertion correctly verifies that only ERROR messages are captured?
PyTest
import logging def test_error_logs_only(caplog): with caplog.at_level(logging.ERROR): logging.warning('Warning message') logging.error('Error message')
Attempts:
2 left
💡 Hint
Logs below ERROR level should not appear in caplog.text.
✗ Incorrect
Since caplog is set to ERROR level, WARNING messages are not captured. The assertion must confirm WARNING is absent and ERROR is present.
🔧 Debug
advanced2:00remaining
Why does this pytest test fail to capture WARNING logs?
This pytest test intends to capture WARNING logs but fails. What is the cause?
PyTest
import logging def test_logs(caplog): logging.warning('Warning message') with caplog.at_level(logging.WARNING): pass assert 'Warning message' in caplog.text
Attempts:
2 left
💡 Hint
Consider when the log message is emitted relative to the caplog context.
✗ Incorrect
caplog.at_level only captures logs emitted inside its context. The warning is logged before entering the context, so it is missed.
❓ framework
advanced2:00remaining
How to configure pytest to capture only ERROR logs globally?
Which pytest configuration snippet sets the global log capture level to ERROR for all tests?
Attempts:
2 left
💡 Hint
Check pytest documentation for log_cli and log_cli_level options.
✗ Incorrect
The correct pytest.ini options to enable live log capture at ERROR level are log_cli=true and log_cli_level=ERROR.
🧠 Conceptual
expert2:00remaining
What is the effect of setting caplog.set_level(logging.INFO) inside a test?
In pytest, what does calling caplog.set_level(logging.INFO) inside a test do?
Attempts:
2 left
💡 Hint
Think about how caplog.set_level affects log capturing during test execution.
✗ Incorrect
caplog.set_level changes the capture level dynamically during the test, affecting logs emitted after the call.