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?✗ Incorrect
Setting
--log-level=ERROR filters logs to show only errors and more severe messages, hiding warnings and info.Which pytest fixture lets you capture and check log messages inside a test?
✗ Incorrect
The
caplog fixture is designed to capture log messages during pytest tests.Why might you want to filter out debug logs during test runs?
✗ Incorrect
Filtering debug logs reduces clutter, making it easier to see warnings and errors.
How do you temporarily set the log level to WARNING inside a pytest test using caplog?
✗ Incorrect
Using
with caplog.at_level(logging.WARNING): temporarily sets the log level inside the test.What is the default log level shown by pytest if not specified?
✗ Incorrect
By default, pytest shows logs at WARNING level and above.
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.