0
0
PyTesttesting~10 mins

caplog for log messages in PyTest - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to capture log messages using caplog.

PyTest
def test_logging(caplog):
    with caplog.at_level([1]):
        logger = logging.getLogger('test')
        logger.info('Test message')
    assert 'Test message' in caplog.text
Drag options to blanks, or click blank then click option'
Alogging.INFO
Blogging.DEBUG
Clogging.ERROR
Dlogging.WARNING
Attempts:
3 left
💡 Hint
Common Mistakes
Using a logging level higher than INFO will not capture the info message.
Not using caplog.at_level context manager.
2fill in blank
medium

Complete the code to check the first captured log message's level name.

PyTest
def test_log_level_name(caplog):
    logger = logging.getLogger('test')
    logger.error('Error occurred')
    assert caplog.records[0].[1] == 'ERROR'
Drag options to blanks, or click blank then click option'
Alevelname
Blevelno
Cmsg
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using levelno which is a number, not a string.
Using msg which is the message text.
3fill in blank
hard

Fix the error in the code to capture debug messages with caplog.

PyTest
def test_debug_log(caplog):
    with caplog.at_level([1]):
        logger = logging.getLogger('test')
        logger.debug('Debug info')
    assert 'Debug info' in caplog.text
Drag options to blanks, or click blank then click option'
Alogging.INFO
Blogging.ERROR
Clogging.DEBUG
Dlogging.CRITICAL
Attempts:
3 left
💡 Hint
Common Mistakes
Setting the level to INFO or higher will miss debug messages.
Not using caplog.at_level context manager.
4fill in blank
hard

Fill both blanks to filter captured logs by level and check message content.

PyTest
def test_filter_logs(caplog):
    logger = logging.getLogger('test')
    logger.warning('Warning message')
    logger.error('Error message')
    filtered = [record for record in caplog.records if record.[1] == [2]]
    assert any('Warning' in record.msg for record in filtered)
Drag options to blanks, or click blank then click option'
Alevelname
Blevelno
C'WARNING'
Dlogging.WARNING
Attempts:
3 left
💡 Hint
Common Mistakes
Using levelno but comparing to a string.
Comparing levelname to a numeric constant.
5fill in blank
hard

Fill all three blanks to capture logs at error level, check message count, and verify message text.

PyTest
def test_error_logs_count(caplog):
    with caplog.at_level([1]):
        logger = logging.getLogger('test')
        logger.error('First error')
        logger.error('Second error')
    errors = [record for record in caplog.records if record.[2] == [3]]
    assert len(errors) == 2
    assert errors[0].msg == 'First error'
Drag options to blanks, or click blank then click option'
Alogging.ERROR
Blevelname
C'ERROR'
Dlevelno
Attempts:
3 left
💡 Hint
Common Mistakes
Using numeric level levelno but comparing to string.
Setting capture level too high or too low.