Challenge - 5 Problems
Log Mastery Badge
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 assertion?
Consider this pytest test function that asserts a log message is emitted at WARNING level.
PyTest
import logging import pytest def func(): logging.warning("Warning issued") def test_log_warning(caplog): with caplog.at_level(logging.WARNING): func() assert "Warning issued" in caplog.text
Attempts:
2 left
💡 Hint
Check if the log message is captured by caplog at the correct level.
✗ Incorrect
The function emits a WARNING log. The test sets caplog to WARNING level and asserts the message is in caplog.text, so the assertion passes.
❓ assertion
intermediate2:00remaining
Which assertion correctly checks for an ERROR log message using caplog?
You want to assert that the log contains the exact message "Error occurred" at ERROR level.
Attempts:
2 left
💡 Hint
caplog.records is a list of log records; check each record's levelname and message.
✗ Incorrect
Option C correctly iterates over all records and checks both levelname and message. Others either check only first record or use wrong attributes.
🔧 Debug
advanced2:00remaining
Why does this pytest log assertion fail unexpectedly?
Given this test code:
import logging
def func():
logging.info("Info message")
def test_log_info(caplog):
with caplog.at_level(logging.WARNING):
func()
assert "Info message" in caplog.text
Why does the assertion fail?
Attempts:
2 left
💡 Hint
Check the log level filter set by caplog.at_level.
✗ Incorrect
caplog.at_level(logging.WARNING) filters out messages below WARNING, so INFO messages are ignored and not captured.
❓ framework
advanced2:00remaining
Which pytest fixture is used to capture log messages during tests?
You want to capture and assert log messages emitted by your code in pytest tests. Which fixture should you use?
Attempts:
2 left
💡 Hint
The fixture name starts with 'cap'.
✗ Incorrect
pytest provides the 'caplog' fixture to capture log messages during tests.
🧠 Conceptual
expert2:00remaining
What is the main benefit of asserting log messages in automated tests?
Why do testers assert log messages in their automated test suites?
Attempts:
2 left
💡 Hint
Think about why logs are useful in software.
✗ Incorrect
Asserting logs helps confirm that the system reports key events and errors, aiding debugging and monitoring.