Skip to content

Commit

Permalink
dump out events on test failures. no longer reset_defaults in setup, …
Browse files Browse the repository at this point in the history
…since it caused a regression in #14. users should mock out their own structlog configuration in tests if necessary
  • Loading branch information
wimglenn committed Mar 26, 2021
1 parent c31f23f commit ae71010
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions pytest_structlog.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import os
import pytest
import structlog


__version__ = "0.3"
__version__ = "0.4"


class EventList(list):
Expand Down Expand Up @@ -59,7 +60,7 @@ def no_op(*args, **kwargs):


@pytest.fixture
def log(monkeypatch):
def log(monkeypatch, request):
"""Fixture providing access to captured structlog events. Interesting attributes:
``log.events`` a list of dicts, contains any events logged during the test
Expand All @@ -73,11 +74,19 @@ def log(monkeypatch):

# redirect logging to log capture
cap = StructuredLogCapture()
structlog.reset_defaults()
structlog.configure(processors=[cap.process])
structlog.configure(processors=[cap.process], cache_logger_on_first_use=False)
monkeypatch.setattr("structlog.configure", no_op)
monkeypatch.setattr("structlog.configure_once", no_op)
request.node.structlog_events = cap.events
yield cap

# back to normal behavior
configure(processors=processors)


@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_call(item):
yield
events = getattr(item, "structlog_events", [])
content = os.linesep.join([str(e) for e in events])
item.add_report_section("call", "structlog", content)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="pytest-structlog",
version="0.3",
version="0.4",
url="https://github.com/wimglenn/pytest-structlog",
description="Structured logging assertions",
long_description=open("README.rst").read(),
Expand Down

0 comments on commit ae71010

Please sign in to comment.