11import json
2+ from collections import defaultdict
23
34import pytest
45from _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