0
0
PyTesttesting~20 mins

Log level filtering in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Log Level Filtering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
ADebug message\nWarning message\nError message\n
BError message\n
CWarning message\nError message\n
DDebug message\n
Attempts:
2 left
💡 Hint
Remember that caplog.at_level sets the minimum log level to capture.
assertion
intermediate
2: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')
Aassert caplog.text == 'Warning message\nError message\n'
Bassert 'Warning message' not in caplog.text and 'Error message' in caplog.text
Cassert caplog.records[0].levelname == 'WARNING'
Dassert 'Warning message' in caplog.text and 'Error message' in caplog.text
Attempts:
2 left
💡 Hint
Logs below ERROR level should not appear in caplog.text.
🔧 Debug
advanced
2: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
AThe warning log is emitted before caplog.at_level context, so it is not captured.
Bcaplog.at_level does not capture WARNING logs by default.
CThe assertion is incorrect; it should check caplog.records instead of caplog.text.
DLogging is disabled globally, so no logs are captured.
Attempts:
2 left
💡 Hint
Consider when the log message is emitted relative to the caplog context.
framework
advanced
2: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?
A
[pytest]
log_cli = true
log_cli_level = ERROR
B
[pytest]
log_cli = true
log_cli_level = WARNING
C
[pytest]
log_capture = true
log_level = ERROR
D
[pytest]
capture_logs = true
capture_level = ERROR
Attempts:
2 left
💡 Hint
Check pytest documentation for log_cli and log_cli_level options.
🧠 Conceptual
expert
2: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?
AIt only affects logs emitted before the call to set_level.
BIt permanently changes the global logging level for all tests to INFO.
CIt disables log capturing for the test.
DIt changes the log capture level dynamically for the rest of the test, capturing INFO and above logs.
Attempts:
2 left
💡 Hint
Think about how caplog.set_level affects log capturing during test execution.