PythiloLesson 9 of 10 · progress kept in this browser

Assert a log record without scraping a console

Logs are structured records before they become formatted console text. Pytest's caplog fixture exposes those records, including logger name, level and rendered message. Testing them is less brittle than matching a timestamped output format.

Set the level inside a bounded context and target the logger used by the component. The fixture restores logging state after the test. A real pytest invocation is important here: calling a test function by hand does not provide fixtures. This example keeps the message deterministic and avoids relying on an application's global logging configuration.

import logging
records = []
class Capture(logging.Handler):
    def emit(self, record):
        records.append(record.getMessage())
log = logging.getLogger("demo.capture")
handler = Capture()
log.addHandler(handler)
log.warning("ready %s", 3)
log.removeHandler(handler)
print(records)
Your task

Implement log_suite() using real pytest with caplog. Capture exactly one INFO record from logger lesson.capture with message "ready 3". Return pytest exit code. The test must inspect records, not console text.

The first run loads pytest into this browser.