0
0
PyTesttesting~5 mins

Log level filtering in PyTest - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is log level filtering in pytest?
Log level filtering in pytest means showing only log messages of a certain importance or higher, like errors or warnings, to keep test output clear and focused.
Click to reveal answer
beginner
How do you set the log level to show only warnings and errors in pytest?
You can set the log level by adding the option --log-level=WARNING when running pytest. This shows only warnings and errors, hiding info and debug messages.
Click to reveal answer
intermediate
What pytest fixture helps capture log messages during tests?
The caplog fixture captures log messages during tests, letting you check if the right messages were logged at the right levels.
Click to reveal answer
beginner
Why is log level filtering useful in automated tests?
It keeps test output clean by hiding less important messages, making it easier to spot real problems and understand test results quickly.
Click to reveal answer
intermediate
Show a simple pytest test snippet that asserts a warning log was created using caplog.
import logging

def test_warning_log(caplog):
    with caplog.at_level(logging.WARNING):
        logging.warning('This is a warning')
    assert 'This is a warning' in caplog.text
Click to reveal answer
What does setting --log-level=ERROR do in pytest?
AShows only error messages and hides warnings and info
BShows all log messages including debug
CShows only info messages
DDisables all logging
Which pytest fixture lets you capture and check log messages inside a test?
Alogcapture
Bcaplog
Clogtest
Dcapturelog
Why might you want to filter out debug logs during test runs?
ATo hide errors
BTo speed up tests
CTo reduce noise and focus on important messages
DTo disable logging completely
How do you temporarily set the log level to WARNING inside a pytest test using caplog?
Alogging.setLevel('WARNING')
Bcaplog.set_level('WARNING')
Ccaplog.warning()
Dwith caplog.at_level(logging.WARNING):
What is the default log level shown by pytest if not specified?
AWARNING
BERROR
CINFO
DDEBUG
Explain how log level filtering helps improve test output readability in pytest.
Think about why seeing fewer messages can help find problems faster.
You got /4 concepts.
    Describe how to use the caplog fixture to check that a warning message was logged during a test.
    Remember the context manager and assertion steps.
    You got /4 concepts.