0
0
PyTesttesting~20 mins

caplog for log messages in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Caplog Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this pytest caplog test?
Consider this pytest test using caplog to capture logs. What will be the value of caplog.records[0].levelname after running the test?
PyTest
import logging

def test_log_level(caplog):
    logger = logging.getLogger('mylogger')
    with caplog.at_level(logging.WARNING):
        logger.error('Error occurred')
    print(caplog.records[0].levelname)
A"WARNING"
B"INFO"
C"ERROR"
D"DEBUG"
Attempts:
2 left
💡 Hint
Remember that logger.error logs at ERROR level, which is higher than WARNING.
assertion
intermediate
2:00remaining
Which assertion correctly checks for a log message using caplog?
You want to assert that the log message 'User created' was logged during the test. Which assertion is correct?
PyTest
import logging

def test_user_creation_logs(caplog):
    logger = logging.getLogger('app')
    logger.info('User created')
Aassert caplog.records[0].msg == 'User created'
Bassert 'User created' in caplog.text
Cassert caplog.records[0].message == 'User created'
Dassert caplog.messages[0] == 'User created'
Attempts:
2 left
💡 Hint
caplog.messages is a list of log messages as strings.
🔧 Debug
advanced
2:00remaining
Why does this caplog test fail to capture logs?
This test does not capture any logs. What is the reason?
PyTest
import logging

def test_no_logs(caplog):
    logger = logging.getLogger('mylogger')
    logger.setLevel(logging.ERROR)
    with caplog.at_level(logging.WARNING):
        logger.info('Info message')
    assert len(caplog.records) == 1
ALogger level is ERROR, so INFO messages are ignored even if caplog level is WARNING
Bcaplog.at_level does not change the logger level, so no logs are captured
Ccaplog only captures WARNING and above by default, so INFO is filtered
DThe assertion is wrong; caplog.records length is 0, not 1
Attempts:
2 left
💡 Hint
Logger's own level controls which messages it emits.
framework
advanced
2:00remaining
How to capture logs from a specific logger using caplog?
You want to capture logs only from the logger named 'myapp.database'. Which caplog usage is correct?
Awith caplog.at_level(logging.INFO, logger='myapp.database'):
Bwith caplog.at_level(logging.INFO): # then filter logs manually
Ccaplog.set_level(logging.INFO, logger='myapp.database')
Dcaplog.set_level(logging.INFO) # captures all loggers
Attempts:
2 left
💡 Hint
caplog.at_level accepts a logger parameter to filter logs.
🧠 Conceptual
expert
2:00remaining
What is the main difference between caplog.records and caplog.messages?
Choose the best explanation of the difference between caplog.records and caplog.messages in pytest.
Acaplog.records contains strings; caplog.messages contains LogRecord objects
Bcaplog.records contains LogRecord objects; caplog.messages contains formatted log strings
Ccaplog.records is a list of logger names; caplog.messages is a list of log levels
Dcaplog.records and caplog.messages are identical lists of log messages
Attempts:
2 left
💡 Hint
Think about raw log data vs formatted text.