Skip to content

Commit 1464f08

Browse files
authored
Include captured warnings in the log (#21)
Fixes #20
1 parent 36c55b6 commit 1464f08

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/pytest_reportlog/plugin.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,23 @@ def pytest_runtest_logreport(self, report):
6767
)
6868
self._write_json_data(data)
6969

70+
def pytest_warning_recorded(self, warning_message, when, nodeid, location):
71+
data = {
72+
"category": (
73+
warning_message.category.__name__ if warning_message.category else None
74+
),
75+
"filename": warning_message.filename,
76+
"lineno": warning_message.lineno,
77+
"message": warning_message.message,
78+
}
79+
extra_data = {
80+
"$report_type": "WarningMessage",
81+
"when": when,
82+
"location": location,
83+
}
84+
data.update(extra_data)
85+
self._write_json_data(data)
86+
7087
def pytest_collectreport(self, report):
7188
data = self._config.hook.pytest_report_to_serializable(
7289
config=self._config, report=report

tests/test_reportlog.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from collections import defaultdict
23

34
import pytest
45
from _pytest.reports import BaseReport
@@ -12,14 +13,19 @@ def test_basics(testdir, tmp_path, pytestconfig):
1213
We don't test the test reports extensively because they have been
1314
tested already in ``test_reports``.
1415
"""
15-
testdir.makepyfile(
16+
p = testdir.makepyfile(
1617
"""
18+
import warnings
19+
1720
def test_ok():
1821
pass
1922
2023
def test_fail():
2124
assert 0
22-
"""
25+
26+
def test_warning():
27+
warnings.warn("message", UserWarning)
28+
"""
2329
)
2430

2531
log_file = tmp_path / "log.json"
@@ -29,7 +35,7 @@ def test_fail():
2935
result.stdout.fnmatch_lines([f"* generated report log file: {log_file}*"])
3036

3137
json_objs = [json.loads(x) for x in log_file.read_text().splitlines()]
32-
assert len(json_objs) == 10
38+
assert len(json_objs) == 14
3339

3440
# first line should be the session_start
3541
session_start = json_objs[0]
@@ -45,6 +51,22 @@ def test_fail():
4551
"$report_type": "SessionFinish",
4652
}
4753

54+
split = defaultdict(list)
55+
for obj in json_objs:
56+
split[obj["$report_type"] == "WarningMessage"].append(obj)
57+
[warning] = split[True]
58+
json_objs = split[False]
59+
60+
assert warning == {
61+
"$report_type": "WarningMessage",
62+
"category": "UserWarning",
63+
"when": "runtest",
64+
"message": "message",
65+
"lineno": 10,
66+
"location": None, # seems to be hard-coded to None
67+
"filename": str(p),
68+
}
69+
4870
# rest of the json objects should be unserialized into report objects; we don't test
4971
# the actual report object extensively because it has been tested in ``test_reports``
5072
# already.
@@ -60,16 +82,21 @@ def test_xdist_integration(testdir, tmp_path):
6082
pytest.importorskip("xdist")
6183
testdir.makepyfile(
6284
"""
85+
import warnings
86+
6387
def test_ok():
6488
pass
6589
6690
def test_fail():
6791
assert 0
68-
"""
92+
93+
def test_warning():
94+
warnings.warn("message", UserWarning)
95+
"""
6996
)
7097
fn = tmp_path / "result.log"
7198
result = testdir.runpytest("-n2", f"--report-log={fn}")
72-
result.stdout.fnmatch_lines("*1 failed, 1 passed*")
99+
result.stdout.fnmatch_lines("*1 failed, 2 passed, 1 warning*")
73100

74101
lines = fn.read_text("UTF-8").splitlines()
75102
data = json.loads(lines[0])

0 commit comments

Comments
 (0)