Skip to main content

An eliot plugin for pytest.

Project description

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.

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-eliot-0.0.2.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

pytest_eliot-0.0.2-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file pytest-eliot-0.0.2.tar.gz.

File metadata

  • Download URL: pytest-eliot-0.0.2.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.2

File hashes

Hashes for pytest-eliot-0.0.2.tar.gz
Algorithm Hash digest
SHA256 51debfd36f941665ba82465f5d53918956e7cf98ceee052f287ee246ef150387
MD5 e584dd1a49c59033e5ddadbad65e84fc
BLAKE2b-256 d60e9479161e02494bcb073222d13b3ea79839b297396dfa5df3019e4c8672c7

See more details on using hashes here.

File details

Details for the file pytest_eliot-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_eliot-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ee4c3004602e1d31248bead94c2aa1115eea416354976efb2009fd2c1b53ac82
MD5 478aba0f7409ab4eb959357f46ec666b
BLAKE2b-256 bc5593340f41620f4dbe9708a612de7273a14c0e3b70ca9418df8af2897314ae

See more details on using hashes here.

Supported by

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