Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

Structured logging assertions

Project description

travis pypi pyversions womm


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

pytest structlog


$ pip install pytest-structlog


The fixture name is log. It has two attributes of interest: 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:

from structlog import get_logger

logger = get_logger()

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

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

from your_lib import spline_reticulator

def test_spline_reticulator(log):
    assert len( == 0
    assert len( == 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 == [
        {"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 membership to check for a single event's data
    assert {"event": "reticulating splines", "level": "info"} in

    # can use >= to specify only the events you're interested in
    assert >= [
        {"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},
    ] <=

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

Project details

Download files

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

Files for pytest-structlog, version 0.4
Filename, size File type Python version Upload date Hashes
Filename, size pytest_structlog-0.4-py2.py3-none-any.whl (4.7 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size pytest-structlog-0.4.tar.gz (5.5 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page