Skip to main content

Structured logging assertions

Project description

actions pypi pyversions womm

pytest-structlog

Structured logging assertions. pytest + structlog = pytest-structlog.

pytest structlog

Installation:

$ pip install pytest-structlog

Usage:

The fixture name is log. It has two attributes of interest: log.events is a list of events from captured log calls, and log.has is a helper function for asserting a single event was logged within the expected context.

Suppose you have some library module, your_lib, which is using structlog:

# your_lib.py
from structlog import get_logger

logger = get_logger()

def spline_reticulator():
    logger.info("reticulating splines")
    for i in range(3):
        logger.debug("processing", spline=i)
    logger.info("reticulated splines", n_splines=3)

Then your test suite might use assertions such as shown below:

# test_your_lib.py
from your_lib import spline_reticulator
import pytest_structlog

def test_spline_reticulator(log: pytest_structlog.StructuredLogCapture):
    assert len(log.events) == 0
    spline_reticulator()
    assert len(log.events) == 5

    # can assert on the event only
    assert log.has("reticulating splines")

    # can assert with subcontext
    assert log.has("reticulated splines")
    assert log.has("reticulated splines", n_splines=3)
    assert log.has("reticulated splines", n_splines=3, level="info")

    # but not incorrect context
    assert not log.has("reticulated splines", n_splines=42)
    assert not log.has("reticulated splines", key="bogus")

    # can assert with the event dicts directly
    assert log.events == [
        {"event": "reticulating splines", "level": "info"},
        {"event": "processing", "level": "debug", "spline": 0},
        {"event": "processing", "level": "debug", "spline": 1},
        {"event": "processing", "level": "debug", "spline": 2},
        {"event": "reticulated splines", "level": "info", "n_splines": 3},
    ]

    # can use friendly factory methods for the events to assert on
    assert log.events == [
        log.info("reticulating splines"),
        log.debug("processing", spline=0),
        log.debug("processing", spline=1),
        log.debug("processing", spline=2),
        log.info("reticulated splines", n_splines=3),
    ]

    # can use membership to check for a single event's data
    assert {"event": "reticulating splines", "level": "info"} in log.events

    # can use >= to specify only the events you're interested in
    assert log.events >= [
        {"event": "processing", "level": "debug", "spline": 0},
        {"event": "processing", "level": "debug", "spline": 2},
    ]

    # or put the comparison the other way around if you prefer
    assert [
        {"event": "processing", "level": "debug", "spline": 0},
        {"event": "processing", "level": "debug", "spline": 2},
    ] <= log.events

    # note: comparisons are order sensitive!
    assert not [
        {"event": "processing", "level": "debug", "spline": 2},
        {"event": "processing", "level": "debug", "spline": 0},
    ] <= log.events

Advanced configuration

By default, pytest-structlog attempts to nerf any pre-existing structlog configuration and set up a list of processors suitable for testing purposes. Sometimes more control over this setup may be desired, for example if the code under test uses custom processors which should be kept even during testing.

For these purposes, the plugin provides options to override the testing processors:

$ pytest --help | grep structlog --after=2
pytest-structlog:
  --structlog-keep=PROCESSOR_NAME
                        Processors to keep if configured (may be specified
                        multiple times).
  --structlog-evict=PROCESSOR_NAME
                        Processors to evict if configured (may be specified
                        multiple times).
...
  structlog_keep (args):
                        Processors to keep if configured (list of names)
  structlog_evict (args):
                        Processors to evict if configured (list of names)

Indicate that some specific processors should be kept during testing with:

pytest --structlog-keep my_processor1 --structlog-keep MyProcessor2

Or write this directly in config files, e.g. in pyproject.toml:

[tool.pytest.ini_options]
structlog_keep = ["my_processor1", "MyProcessor2"]

Sometimes, instead of listing processors to keep, it may be more convenient to list which processors to exclude during testing. In this case you may specify an eviction list:

[tool.pytest.ini_options]
structlog_evict = ["TimeStamper", "JSONRender"]

You may only use "keep" or "evict" mode. It is an error to specify both.

For complete control over which processors should be used in testing, the best way would be to add a structlog.configure() call directly in your conftest.py file and use --structlog-explicit (or set structlog_explicit = true) when running pytest to disable automatic processors selection entirely.

Using pytest -v or pytest -vv you can see more details about which processors pytest-structlog has included or excluded during the test startup.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_structlog-1.0.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_structlog-1.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file pytest_structlog-1.0.tar.gz.

File metadata

  • Download URL: pytest_structlog-1.0.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for pytest_structlog-1.0.tar.gz
Algorithm Hash digest
SHA256 530c4ccbee5fd617dd1d101138d3f7078ecb31b26cb4bf75ba3361b4d0b9fc45
MD5 15f8a72c8ae9495ae071a1849aca5812
BLAKE2b-256 fac0e10f6cc6e3e8feb1152d5e280295cab9dbfb0ee04f0fb86f8ce47d0a2851

See more details on using hashes here.

File details

Details for the file pytest_structlog-1.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_structlog-1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for pytest_structlog-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d3ff7e37c0b98500dc6c8bc7ff3b289f1309c4ec83a8c054c3726d013e483cff
MD5 228c5a700ffe1057fc6cb938b83dfc2e
BLAKE2b-256 0bc48e4dd71c203c6af2d6d2a1313f7f0c58175eefc4186fb7b6452f0928c6d4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page