We check log messages to make sure our program reports important events correctly. This helps us find problems and understand what happened during a test.
Asserting log messages in PyTest
with caplog.at_level(logging.LEVEL): # code that triggers logging assert 'expected message' in caplog.text
caplog is a pytest fixture that captures log messages during tests.
You can set the log level with caplog.at_level() to capture messages of that level or higher.
def test_warning_log(caplog): import logging with caplog.at_level(logging.WARNING): logging.warning('This is a warning') assert 'This is a warning' in caplog.text
def test_error_log(caplog): import logging with caplog.at_level(logging.ERROR): logging.error('Error happened') assert any('Error happened' in msg for msg in caplog.messages)
This test runs a function that logs messages at different levels. It then checks that all these messages appear in the captured logs.
import logging import pytest def function_that_logs(): logging.info('Starting process') logging.warning('A warning occurred') logging.error('An error occurred') def test_logs(caplog): with caplog.at_level(logging.INFO): function_that_logs() assert 'Starting process' in caplog.text assert 'A warning occurred' in caplog.text assert 'An error occurred' in caplog.text if __name__ == '__main__': pytest.main([__file__])
Always import logging to use log levels and log messages.
Use caplog.text to get all captured logs as one string, or caplog.records for detailed log records.
Set the log level carefully to capture the messages you want.
Use caplog fixture to capture logs in pytest tests.
Check log messages with assertions on caplog.text or caplog.messages.
Set log level with caplog.at_level() to control which messages are captured.
Practice
caplog fixture in pytest when testing log messages?Solution
Step 1: Understand the role of
Thecaplogcaplogfixture is designed to capture log messages during pytest test runs.Step 2: Identify correct usage context
It helps verify that expected log messages are produced, which is essential for testing logging behavior.Final Answer:
To capture log messages generated during test execution -> Option AQuick Check:
caplogcaptures logs [OK]
- Confusing caplog with mocking tools
- Thinking caplog measures time
- Assuming caplog generates data
caplog in pytest?Solution
Step 1: Understand log levels
To capture WARNING messages but not INFO, the log level must be set to WARNING.Step 2: Identify correct method call
Usingcaplog.set_level(logging.WARNING)sets the capture level to WARNING, capturing WARNING and above.Final Answer:
caplog.set_level(logging.WARNING) -> Option DQuick Check:
Set level to WARNING to capture warnings [OK]
- Setting level too high to capture lower severity logs
- Using incorrect logging level constants
- Not calling set_level at all
import logging
def test_log_message(caplog):
with caplog.at_level(logging.ERROR):
logging.error('Error occurred')
assert 'Error occurred' in caplog.textSolution
Step 1: Analyze log capture level
The test sets caplog to capture ERROR level logs, and an error message is logged.Step 2: Check assertion logic
The assertion checks if the logged message is incaplog.text, which contains all captured logs as text.Final Answer:
Test passes because the error message is captured -> Option AQuick Check:
Logged ERROR message found in caplog.text [OK]
- Assuming caplog.text is empty without setting level
- Confusing caplog.records with caplog.text
- Forgetting to set log level
caplog to assert a log message:
import logging
def test_info_log(caplog):
logging.info('Info issued')
assert 'Info issued' in caplog.textSolution
Step 1: Check log level setting
The test logs an INFO but does not setcaplogto capture INFO level logs, so no logs are captured.Step 2: Understand impact on assertion
Because no logs are captured,caplog.textis empty and the assertion fails.Final Answer:
Missing setting log level to capture INFO messages -> Option CQuick Check:
Set caplog level to INFO before logging [OK]
- Not using caplog.at_level or caplog.set_level
- Assuming logs are captured by default
- Incorrectly placing assertion before logging
caplog assertion correctly verifies this?Solution
Step 1: Understand caplog.records structure
caplog.recordsis a list of log record objects with attributes likemessageandlevelname.Step 2: Choose correct assertion logic
assert any('success' in record.message for record in caplog.records if record.levelname == 'INFO') checks if any INFO level record contains 'success' in its message, which correctly verifies the log content.Final Answer:
assert any('success' in record.message for record in caplog.records if record.levelname == 'INFO') -> Option BQuick Check:
Use caplog.records with message and levelname for assertions [OK]
- Using count on records list incorrectly
- Comparing caplog.text directly to a string
- Accessing caplog.records elements as strings
Ready to prove it?
Pass the test and this pattern moves unknown
