1
1
import json
2
+ from collections import defaultdict
2
3
3
4
import pytest
4
5
from _pytest .reports import BaseReport
@@ -12,14 +13,19 @@ def test_basics(testdir, tmp_path, pytestconfig):
12
13
We don't test the test reports extensively because they have been
13
14
tested already in ``test_reports``.
14
15
"""
15
- testdir .makepyfile (
16
+ p = testdir .makepyfile (
16
17
"""
18
+ import warnings
19
+
17
20
def test_ok():
18
21
pass
19
22
20
23
def test_fail():
21
24
assert 0
22
- """
25
+
26
+ def test_warning():
27
+ warnings.warn("message", UserWarning)
28
+ """
23
29
)
24
30
25
31
log_file = tmp_path / "log.json"
@@ -29,7 +35,7 @@ def test_fail():
29
35
result .stdout .fnmatch_lines ([f"* generated report log file: { log_file } *" ])
30
36
31
37
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
33
39
34
40
# first line should be the session_start
35
41
session_start = json_objs [0 ]
@@ -45,6 +51,22 @@ def test_fail():
45
51
"$report_type" : "SessionFinish" ,
46
52
}
47
53
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
+
48
70
# rest of the json objects should be unserialized into report objects; we don't test
49
71
# the actual report object extensively because it has been tested in ``test_reports``
50
72
# already.
@@ -60,16 +82,21 @@ def test_xdist_integration(testdir, tmp_path):
60
82
pytest .importorskip ("xdist" )
61
83
testdir .makepyfile (
62
84
"""
85
+ import warnings
86
+
63
87
def test_ok():
64
88
pass
65
89
66
90
def test_fail():
67
91
assert 0
68
- """
92
+
93
+ def test_warning():
94
+ warnings.warn("message", UserWarning)
95
+ """
69
96
)
70
97
fn = tmp_path / "result.log"
71
98
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 *" )
73
100
74
101
lines = fn .read_text ("UTF-8" ).splitlines ()
75
102
data = json .loads (lines [0 ])
0 commit comments