0
0
PyTesttesting~10 mins

Log level filtering in PyTest - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks that only log messages at or above the WARNING level are captured during test execution. It verifies that INFO level messages are filtered out and do not appear in the captured logs.

Test Code - pytest
PyTest
import logging
import pytest

def test_log_level_filtering(caplog):
    caplog.set_level(logging.WARNING)
    logging.getLogger().info("This is an info message")
    logging.getLogger().warning("This is a warning message")
    assert "This is an info message" not in caplog.text
    assert "This is a warning message" in caplog.text
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startspytest test runner initialized-PASS
2caplog fixture sets log capture level to WARNINGLogging system configured to capture WARNING and above-PASS
3Logger emits INFO level message: 'This is an info message'INFO message generated but below capture level-PASS
4Logger emits WARNING level message: 'This is a warning message'WARNING message generated and captured-PASS
5Assert INFO message is not in captured logsCaptured logs do not contain INFO messageassert "This is an info message" not in caplog.textPASS
6Assert WARNING message is in captured logsCaptured logs contain WARNING messageassert "This is a warning message" in caplog.textPASS
Failure Scenario
Failing Condition: caplog does not filter out INFO messages or fails to capture WARNING messages
Execution Trace Quiz - 3 Questions
Test your understanding
What log level is set for capturing logs in this test?
AERROR
BINFO
CWARNING
DDEBUG
Key Result
Always set the log capture level explicitly in tests to filter out unwanted log messages and verify only relevant logs appear.