Bird
Raised Fist0
PyTesttesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using caplog in pytest tests?
easy
A. To capture and check log messages generated during test execution
B. To mock external API calls in tests
C. To measure test execution time
D. To generate test reports automatically

Solution

  1. Step 1: Understand caplog's role in pytest

    caplog is a pytest fixture used to capture log messages during tests.
  2. Step 2: Identify the main use

    It allows assertions on log output to verify correct logging behavior.
  3. Final Answer:

    To capture and check log messages generated during test execution -> Option A
  4. Quick Check:

    caplog = capture logs [OK]
Hint: caplog captures logs so you can check messages easily [OK]
Common Mistakes:
  • Confusing caplog with mocking tools
  • Thinking caplog measures time
  • Assuming caplog generates reports
2. Which of the following is the correct way to use caplog to capture only WARNING level logs in a test function?
easy
A. caplog.log_level(logging.WARNING)
B. caplog.set_level(logging.WARNING)
C. caplog.capture_level = logging.WARNING
D. with caplog.at_level(logging.WARNING):

Solution

  1. Step 1: Recall caplog syntax for setting log level

    caplog uses a context manager method at_level() to set the log level temporarily.
  2. Step 2: Match correct syntax

    The correct syntax is with caplog.at_level(logging.WARNING): to capture warnings.
  3. Final Answer:

    with caplog.at_level(logging.WARNING): -> Option D
  4. Quick Check:

    Use caplog.at_level() for log level [OK]
Hint: Use with caplog.at_level(level) to set capture level [OK]
Common Mistakes:
  • Trying to set level by assignment
  • Using non-existent caplog methods
  • Forgetting to use 'with' for context
3. Given the following test code, what will be the output of the assertion?
import logging

def test_log_message(caplog):
    with caplog.at_level(logging.INFO):
        logging.info('Test started')
    assert 'Test started' in caplog.text
medium
A. The assertion passes because the log message is captured
B. The assertion fails because caplog.text is empty
C. The test raises a NameError due to missing import
D. The assertion fails because logging level is too low

Solution

  1. Step 1: Analyze log capture level

    The code sets caplog to capture INFO level logs, which includes the info message logged.
  2. Step 2: Check assertion on caplog.text

    The message 'Test started' is logged and appears in caplog.text, so the assertion passes.
  3. Final Answer:

    The assertion passes because the log message is captured -> Option A
  4. Quick Check:

    Logged message in caplog.text = assertion pass [OK]
Hint: caplog.text holds captured logs as a string [OK]
Common Mistakes:
  • Assuming caplog.text is empty without at_level
  • Confusing log levels and capture
  • Forgetting to import logging
4. Identify the error in the following pytest test using caplog:
import logging

def test_error_log(caplog):
    caplog.at_level(logging.ERROR)
    logging.error('An error occurred')
    assert 'An error occurred' in caplog.text
medium
A. logging.error() is called before caplog is imported
B. caplog.at_level() must be used as a context manager with 'with'
C. assertion should check caplog.records instead of caplog.text
D. logging level ERROR is too high to capture

Solution

  1. Step 1: Check usage of caplog.at_level()

    caplog.at_level() is a context manager and must be used with 'with' keyword.
  2. Step 2: Identify missing 'with' statement

    The code calls caplog.at_level() without 'with', so it does not set the level properly.
  3. Final Answer:

    caplog.at_level() must be used as a context manager with 'with' -> Option B
  4. Quick Check:

    Use 'with caplog.at_level()' not just call [OK]
Hint: Always use 'with' for caplog.at_level() context [OK]
Common Mistakes:
  • Calling caplog.at_level() without 'with'
  • Checking wrong caplog attribute
  • Misunderstanding log level capture
5. You want to test that your function logs a WARNING message only when a certain condition is true. Which pytest test code correctly uses caplog to check this behavior?
hard
A. def test_warn_log(caplog): with caplog.at_level(logging.WARNING): my_function(True) assert 'Warning triggered' in caplog.text with caplog.at_level(logging.WARNING): my_function(False) assert 'Warning triggered' not in caplog.text
B. def test_warn_log(caplog): with caplog.at_level(logging.INFO): my_function(True) assert 'Warning triggered' in caplog.text
C. def test_warn_log(caplog): with caplog.at_level(logging.WARNING): my_function(True) my_function(False) assert caplog.text.count('Warning triggered') == 1
D. def test_warn_log(caplog): caplog.set_level(logging.WARNING) my_function(True) assert 'Warning triggered' in caplog.text my_function(False) assert 'Warning triggered' not in caplog.text

Solution

  1. Step 1: Understand the requirement

    We want to check that the warning message logs only once when condition is true, not when false.
  2. Step 2: Analyze options for correct caplog usage

    def test_warn_log(caplog): with caplog.at_level(logging.WARNING): my_function(True) my_function(False) assert caplog.text.count('Warning triggered') == 1 uses one context capturing both calls and asserts the warning appears exactly once, matching the requirement.
  3. Step 3: Why other options fail

    def test_warn_log(caplog): with caplog.at_level(logging.WARNING): my_function(True) assert 'Warning triggered' in caplog.text with caplog.at_level(logging.WARNING): my_function(False) assert 'Warning triggered' not in caplog.text: separate contexts but caplog.text accumulates logs, so second assert 'Warning triggered' not in caplog.text fails (message from first call persists); def test_warn_log(caplog): caplog.set_level(logging.WARNING) my_function(True) assert 'Warning triggered' in caplog.text my_function(False) assert 'Warning triggered' not in caplog.text: caplog.set_level does not exist (AttributeError), and accumulation would fail second assert; def test_warn_log(caplog): with caplog.at_level(logging.INFO): my_function(True) assert 'Warning triggered' in caplog.text: tests only True case with INFO level (captures WARNING but no False verification).
  4. Final Answer:

    def test_warn_log(caplog): with caplog.at_level(logging.WARNING): my_function(True) my_function(False) assert caplog.text.count('Warning triggered') == 1 -> Option C
  5. Quick Check:

    Use one caplog context and count messages [OK]
Hint: Use one with caplog.at_level() block and count messages [OK]
Common Mistakes:
  • Using multiple caplog contexts losing logs
  • Using wrong log level in at_level
  • Assuming caplog.set_level exists
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard