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
✗ 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?
Alogcapture
Bcaplog
Clogtest
Dcapturelog
✗ 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?
ATo hide errors
BTo speed up tests
CTo reduce noise and focus on important messages
DTo disable logging completely
✗ 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?
Alogging.setLevel('WARNING')
Bcaplog.set_level('WARNING')
Ccaplog.warning()
Dwith caplog.at_level(logging.WARNING):
✗ 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?
AWARNING
BERROR
CINFO
DDEBUG
✗ 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.
Practice
(1/5)
1. What does caplog.at_level() do in pytest?
easy
A. It sets the minimum log level to capture during a test.
B. It clears all captured logs before the test runs.
C. It disables logging for the entire test session.
D. It changes the log message format.
Solution
Step 1: Understand caplog usage
caplog is a pytest fixture used to capture log messages during tests.
Step 2: Purpose of at_level()
The method at_level() sets the minimum log level to capture, filtering out lower-level messages.
Final Answer:
It sets the minimum log level to capture during a test. -> Option A
Quick Check:
caplog.at_level() sets log capture level [OK]
Hint: Remember: at_level sets minimum log level to capture [OK]
Common Mistakes:
Thinking it clears logs instead of setting level
Confusing it with disabling logging
Assuming it changes log format
2. Which of the following is the correct syntax to capture only WARNING and above logs in pytest using caplog?
easy
A. with caplog.at_level(logging.WARNING):
B. with caplog.set_level(logging.WARNING):
C. caplog.at_level = logging.WARNING
D. caplog.capture_level(logging.WARNING)
Solution
Step 1: Identify correct method usage
The correct method to set log level context is caplog.at_level() used with with statement.
Step 2: Check syntax correctness
with caplog.at_level(logging.WARNING): uses with caplog.at_level(logging.WARNING): which is the documented and correct syntax.
Final Answer:
with caplog.at_level(logging.WARNING): -> Option A
Quick Check:
Use with caplog.at_level(level) for filtering [OK]
Hint: Use 'with caplog.at_level(level):' to filter logs [OK]
Common Mistakes:
Using assignment instead of context manager
Using non-existent methods like set_level or capture_level
Missing the with statement
3. Given the code below, what log messages will be captured?
B. caplog.at_level() must be used as a context manager with 'with'.
C. caplog.text does not exist; use caplog.records instead.
D. Logging messages must be uppercase strings.
Solution
Step 1: Check caplog.at_level usage
The method at_level() must be used with a with statement to set the log level context.
Step 2: Identify the error
Calling caplog.at_level(logging.INFO) without with does not set the level properly, so logs may not be filtered as expected.
Final Answer:
caplog.at_level() must be used as a context manager with 'with'. -> Option B
Quick Check:
Use 'with caplog.at_level(level):' always [OK]
Hint: Always use 'with' when calling caplog.at_level() [OK]
Common Mistakes:
Calling at_level() without 'with' statement
Assuming caplog.text is invalid (it exists)
Thinking assert syntax is wrong
5. You want to test a function that logs messages at DEBUG, INFO, and ERROR levels. You only want to capture INFO and ERROR logs but exclude DEBUG. Which pytest code snippet correctly sets this up?
hard
A. with caplog.at_level(logging.ERROR):
func()
assert 'INFO message' in caplog.text
B. with caplog.at_level(logging.DEBUG):
func()
assert 'INFO message' in caplog.text
C. with caplog.at_level(logging.INFO):
func()
assert 'DEBUG message' in caplog.text
D. with caplog.at_level(logging.INFO):
func()
assert 'DEBUG message' not in caplog.text
Solution
Step 1: Determine log levels to capture
Setting caplog.at_level(logging.INFO) captures INFO and ERROR logs but excludes DEBUG logs.
Step 2: Verify assertions
with caplog.at_level(logging.INFO):
func()
assert 'DEBUG message' not in caplog.text asserts DEBUG messages are not in captured logs, which matches the requirement.
Final Answer:
with caplog.at_level(logging.INFO):
func()
assert 'DEBUG message' not in caplog.text -> Option D
Quick Check:
Set level INFO to exclude DEBUG logs [OK]
Hint: Set level to INFO to exclude DEBUG logs [OK]
Common Mistakes:
Setting level too low and capturing DEBUG logs
Setting level too high and missing INFO logs
Wrong assertion about presence of DEBUG logs
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.