Skip to content

Commit 76dad35

Browse files
authored
Added a log.count helper method (#40)
* Added a log.count helper method * mypy exclude tests * ditch the install check * strict editable * tidy
1 parent 1bbad61 commit 76dad35

5 files changed

Lines changed: 37 additions & 17 deletions

File tree

.github/workflows/tests.yml

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,22 @@ jobs:
2121
- "3.10"
2222
- "3.11"
2323
- "3.12"
24-
24+
- "3.13"
2525
steps:
2626
- uses: actions/checkout@v4
2727
- uses: actions/setup-python@v5
2828
with:
2929
python-version: ${{ matrix.python-version }}
3030
- name: Install
31-
run: |
32-
set -xe
33-
python -m pip install --editable .
31+
run: pip install --editable . --config-settings editable_mode=strict
3432

3533
- name: Run tests for ${{ matrix.python-version }}
36-
run: python -m pytest
34+
run: pytest
3735

3836
- name: Run mypy checks for ${{ matrix.python-version }}
3937
run: |
40-
python -m pip install mypy
41-
python -m mypy pytest_structlog
42-
43-
- name: Run mypy install checks for ${{ matrix.python-version }}
44-
run: |
45-
# This checks that things like the py.typed bits work
46-
cd tests
47-
python -m pip install ..
48-
python -m mypy .
38+
pip install mypy
39+
mypy pytest_structlog
4940
5041
- uses: jakebailey/pyright-action@v2
5142
with:

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ Then your test suite might use assertions such as shown below:
4141

4242
``` python
4343
# test_your_lib.py
44+
from pytest_structlog import StructuredLogCapture
45+
4446
from your_lib import spline_reticulator
45-
import pytest_structlog
4647

47-
def test_spline_reticulator(log: pytest_structlog.StructuredLogCapture):
48+
49+
def test_spline_reticulator(log: StructuredLogCapture):
4850
assert len(log.events) == 0
4951
spline_reticulator()
5052
assert len(log.events) == 5
@@ -99,6 +101,9 @@ def test_spline_reticulator(log: pytest_structlog.StructuredLogCapture):
99101
{"event": "processing", "level": "debug", "spline": 2},
100102
{"event": "processing", "level": "debug", "spline": 0},
101103
] <= log.events
104+
105+
# count of events
106+
assert log.count("processing") == 3
102107
```
103108

104109
## Advanced configuration

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "pytest-structlog"
7-
version = "1.1"
7+
version = "1.2"
88
description = "Structured logging assertions"
99
readme = "README.md"
1010
classifiers = [
@@ -44,3 +44,4 @@ warn_unused_ignores = true
4444
warn_return_any = true
4545
warn_unreachable = true
4646
strict_equality = true
47+
exclude = "tests"

pytest_structlog/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ def has(self, message: str, **context: Any) -> bool:
9797
context["event"] = message
9898
return any(is_submap(context, e) for e in self.events)
9999

100+
def count(self, message: str, **context: Any) -> int:
101+
"""Returns the number of messages logged, with optional
102+
subcontext. Usage in test code would be with an assertion, e.g.:
103+
104+
assert log.count("foo") == 2
105+
assert log.count("bar", k1="v1", k2="v2") == 1
106+
"""
107+
context["event"] = message
108+
return sum(is_submap(context, e) for e in self.events)
109+
100110
def log(self, level: Union[int, str], event: str, **kw: Any) -> dict[str, Any]:
101111
"""Create log event to assert against."""
102112
return dict(level=level_to_name(level), event=event, **kw)

tests/test_issue39.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import structlog
2+
3+
from pytest_structlog import StructuredLogCapture
4+
5+
logger = structlog.get_logger()
6+
7+
8+
def test_count(log: StructuredLogCapture):
9+
for i in range(3):
10+
logger.info("hello iteration", it=i)
11+
assert log.count("hello iteration") == 3
12+
assert log.count("hello iteration", it=1) == 1
13+
assert log.count("hello iteration", k=1) == 0

0 commit comments

Comments
 (0)