Skip to main content

Pytest-eliot

Fixtures to use eliot from pytest.

Testing your logging

pytest-eliot allows using eliot with pytest. The usage is kind of different and adapted to pytest style.

https://eliot.readthedocs.io/en/stable/generating/testing.html

Linting your logs

pytest-eliot provides a fixture that can be used as a context manager. The eliot capture_logging is provided as the fixture eliot_capture_logging.

The context manager will ensure that:

  1. You haven't logged anything that isn't JSON serializable.
  2. There are no unexpected tracebacks, indicating a bug somewhere in your code.
def test_mytest(eliot_capture_logging):
    with eliot_capture_logging():
        call_my_function()

Making assertions about the logs

You can also ensure the correct messages were logged.

from eliot import log_message

class UserRegistration(object):

    def __init__(self):
        self.db = {}

    def register(self, username, password, age):
        self.db[username] = (password, age)
        log_message(message_type="user_registration",
                    username=username, password=password,
                    age=age)

Here's how we'd test it:

from myapp.registration import UserRegistration

def test_registration(eliot_capture_logging):
    registry = UserRegistration()

    with eliot_capture_logging() as logger:
        registry.register("john", "password", 12)

        msg = logger.messages[0]

        fields = {"username": "john",
                  "password": "password",
                  "age": 12}

        assert fields.items() <= msg.items()  # Fields items is a subset of msg items.

    assert registry.db["john"] == ("password", 12)

Testing tracebakcs

Eliot provides utilities for making assertions about the structure of individual messages and actions. The simplest method is using the assertHasMessage utility function which asserts that a message of a given message type has the given fields:

def test_badpath(eliot_capture_logging):
    mything = MyThing()

    with eliot_capture_logging() as logger:
        mything.load("/nonexistant/path")

        messages = logger.flush_tracebacks(OSError)
        assert len(messages) == 1

Testing Message and Action Structure

pytest-eliot provides utilities for making assertions about the structure of individual messages and actions. The simplest method is using the eliot_has_message utility function which asserts that a message of a given message type has the given fields:

def test_registration(eliot_capture_logging, eliot_has_message):
    with eliot_capture_logging() as logger:
        registry = UserRegistration()
        registry.register("john", "password", 12)

        assert eliot_has_message(
            logger, message_type="user_registration",
            fields={
                "username": "john",
                "password:" "password",
                "age": 12
            }
        )

Custom JSON encoding

Custom testing setup

Must wrap all low level functions here as fixtures.

Metadata

Release files for pytest-eliot 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-eliot 0.0.3
File Size Uploaded
pytest-eliot-0.0.3.tar.gz 7.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-eliot 0.0.3
File Interpreter ABI Platform
pytest_eliot-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 15.4 kB

Release files / pytest-eliot-0.0.3.tar.gz

Download URL pytest-eliot-0.0.3.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e57bc8f73250455ff066d96b120fc0076ec91c746bf8182c43508005144ea4b0
BLAKE2b-256 checksum
How to use checksums
a278815f7a7eb97261385a8ccf253c6a027a01388a4779fad5d08836c8484ec2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.2

Release files / pytest_eliot-0.0.3-py3-none-any.whl

Download URL pytest_eliot-0.0.3-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1fce78a7256726cd11f4b5bc2af828cc2ef63f3be5e88646cb7e7890f30f32d7
BLAKE2b-256 checksum
How to use checksums
5c0e0a80db71033a711bb9967a7ab4507e44f7f22d9a4dc5c5cbdeeca10d3d0d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.2

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page