0
0
PyTesttesting~15 mins

Log level filtering in PyTest - Build an Automation Script

Choose your learning style9 modes available
Verify log messages are filtered by log level
Preconditions (2)
Step 1: Configure the logger to capture logs at INFO level
Step 2: Call the function that logs messages at DEBUG, INFO, WARNING, and ERROR levels
Step 3: Capture the logs during the function execution
Step 4: Verify that DEBUG messages are not present in the captured logs
Step 5: Verify that INFO, WARNING, and ERROR messages are present in the captured logs
✅ Expected Result: Only INFO, WARNING, and ERROR log messages are captured; DEBUG messages are filtered out
Automation Requirements - pytest
Assertions Needed:
Assert DEBUG messages are not in captured logs
Assert INFO messages are in captured logs
Assert WARNING messages are in captured logs
Assert ERROR messages are in captured logs
Best Practices:
Use pytest's caplog fixture to capture logs
Set logger level explicitly before test
Use clear and specific assertions for log content
Avoid hardcoding log message strings; use variables or constants
Automated Solution
PyTest
import logging
import pytest

# Function that logs messages at different levels
def log_messages():
    logger = logging.getLogger('test_logger')
    logger.debug('Debug message')
    logger.info('Info message')
    logger.warning('Warning message')
    logger.error('Error message')


def test_log_level_filtering(caplog):
    logger = logging.getLogger('test_logger')
    logger.setLevel(logging.INFO)  # Set log level to INFO

    with caplog.at_level(logging.INFO, logger='test_logger'):
        log_messages()

    # Extract captured log messages
    messages = [record.getMessage() for record in caplog.records]

    # Assertions
    assert 'Debug message' not in messages, 'DEBUG messages should be filtered out'
    assert 'Info message' in messages, 'INFO message should be captured'
    assert 'Warning message' in messages, 'WARNING message should be captured'
    assert 'Error message' in messages, 'ERROR message should be captured'

The log_messages function logs messages at four levels.

In the test test_log_level_filtering, we set the logger level to INFO to filter out DEBUG messages.

We use pytest's caplog fixture to capture logs during the function call.

After running the function, we collect all log messages and assert that DEBUG messages are not present, while INFO, WARNING, and ERROR messages are present.

This approach ensures the log level filtering works as expected.

Common Mistakes - 4 Pitfalls
Not setting the logger level explicitly before the test
Using print statements instead of logging
{'mistake': 'Not using caplog fixture and trying to capture logs manually', 'why_bad': "Manual capturing is error-prone and more complex than using pytest's built-in caplog fixture.", 'correct_approach': 'Use caplog fixture to capture logs cleanly and reliably.'}
Hardcoding log message strings in assertions without matching exact messages
Bonus Challenge

Now add data-driven testing with 3 different logger levels: DEBUG, INFO, WARNING. Verify that only messages at or above the set level are captured.

Show Hint