skillfed

pytest-structlog

Structured logging assertions

pytest-structlog v1.2 286.5K downloads/30d#8,041 on PyPI67
Permissive license MIT Active released

What it is and what it does

pytest-structlog is a pytest plugin that integrates with structlog to capture and inspect structured log events during testing. It provides a `log` fixture that exposes captured events as a list and offers helper methods like `log.has()` for asserting that specific log messages with particular context were emitted. The fixture automatically configures structlog for testing purposes, stripping away production processors and replacing them with a test-friendly list-based collector.

The package lets you verify not just that logging occurred, but exactly what was logged—including event messages, log levels, and arbitrary context fields. You can assert on individual events, compare sequences of events, count occurrences, and use flexible matching patterns. It also provides command-line options and configuration file support to customize which structlog processors are kept or removed during testing, allowing you to preserve custom processors when needed.

Use it for:

  • Verify that your application logs the correct messages and context when specific code paths execute.
  • Assert that errors or warnings are logged with the expected fields and severity levels during exception handling.
  • Test that batch operations log the right number of events and include required tracking or debugging information.
  • Validate that custom structlog processors are invoked correctly by inspecting the final event dictionaries.
  • Ensure sensitive information is not accidentally logged by checking event contents in security-focused tests.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Provides a pytest fixture for capturing and asserting on structured logs produced by structlog, enabling test-friendly inspection of log events and their context.

Yes. Low install friction, active maintenance, no known vulnerabilities, and MIT licensing make it a safe choice. If your test suite uses structlog for structured logging, this fixture is the standard way to assert on log output in pytest—it directly addresses a gap in testing structured logs that would otherwise require manual mocking or post-test log file inspection.

Install

pytest-structlog on PyPI

pip

pip install pytest-structlog

uv

uv add pytest-structlog

poetry

poetry add pytest-structlog

Installing pytest-structlog

Before you install

Low friction: pure Python wheel with only pytest and structlog as runtime dependencies. Actively maintained with a recent release (338 days ago) and an active repository.

License in practice

MIT license permits use in proprietary and open-source projects with minimal restrictions; attribution required.

Quickstart

pip install pytest-structlog

# In your test file:
from pytest_structlog import StructuredLogCapture

def test_logging(log: StructuredLogCapture):
    # Your code that logs via structlog
    assert log.has("event message")
    assert len(log.events) == 1

Requires Python 3.8 or later; structlog must be configured in the code under test.

Verify before relying

  • Whether the fixture automatically captures all structlog output or requires explicit configuration in code under test.
  • Performance impact when capturing large numbers of log events in a single test.

Package facts

License MIT (permissive)
Python support supports the current Python release (>=3.8)
Install friction low — pure-Python wheel
Runtime dependencies 2 — pytest, structlog
Maintenance actively maintained — 338 days since the last release
Last repo commit
First released
Downloads 286,541/month — #8,041 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: pytest_structlog-1.2-py3-none-any.whl

Framework :: PytestProgramming Language :: PythonProgramming Language :: Python :: 3

Tags

pytest structlog fixturestructured logging test assertionscapture structlog events in testspytest log verificationstructlog test helperassert on structured logspytest logging fixture
pytest-pluginstructured-loggingtest-assertions

More Testing packages

pluggy

Pluggy provides a plugin system that lets you…

permissive · top 100 on PyPI

pytest

pytest is a testing framework that lets you…

permissive · top 100 on PyPI

virtualenv

virtualenv creates isolated Python environments…

permissive · top 100 on PyPI

coverage

Coverage.py measures which lines of Python code…

permissive · top 1,000 on PyPI

pytest-asyncio

pytest-asyncio is a pytest plugin that enables…

permissive · top 1,000 on PyPI

pytest-json-ctrf

A pytest plugin that generates test reports in…

permissive · top 1,000 on PyPI

structlog-gcp

Configures structlog to output logs in Google…

unclear · top 15,000 on PyPI

structlog-sentry

Bridges structlog and Sentry to automatically…

permissive · top 5,000 on PyPI

opentelemetry-instrumentation-structlog

Integrates structlog logging with OpenTelemetry…

permissive · top 15,000 on PyPI

structlog

structlog is a structured logging library that…

permissive · top 1,000 on PyPI

django-structlog

Integrates structured logging into Django…

permissive · top 5,000 on PyPI

macwinnie-enhanced-logging

Provides a batteries-included logging setup…

unclear · top 15,000 on PyPI

structlog-pretty

Adds structlog processors that format log…

permissive · top 15,000 on PyPI

pytest-harvest

pytest-harvest captures and stores data…

permissive · top 15,000 on PyPI

pytest-loguru

Extends pytest's caplog fixture to capture and…

permissive · top 15,000 on PyPI

pytest-assume

A pytest plugin that collects and reports…

permissive · top 15,000 on PyPI