Structured logging assertions
Project description
pytest-structlog
Structured logging assertions. 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
def test_spline_reticulator(log):
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 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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest-structlog-0.3.tar.gz.
File metadata
- Download URL: pytest-structlog-0.3.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be01025a712f03e9e2ede3bfbd9bce7ce79d4b7449d426fc603bebfcb0b7e860
|
|
| MD5 |
dd64ba6c3056c9e654f8b968288b7d4b
|
|
| BLAKE2b-256 |
4d709750d441fe2170963e2af0e8c358e41aa8dad6ae01d0617f4a8d19d3dddb
|
File details
Details for the file pytest_structlog-0.3-py2.py3-none-any.whl.
File metadata
- Download URL: pytest_structlog-0.3-py2.py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c07ab9c89ee942ca5776ac81c746387dcb26d52c53466ee850daab67bb5b0281
|
|
| MD5 |
e54ffed1118128eac37958a67c9510db
|
|
| BLAKE2b-256 |
113e310ef54a21ce3d9306d95eeec85ca18f85d7694fe85517f2a621c480efd2
|