Skip to content

Commit ceef0af

Browse files
committed
Improve coverage for to_json() with paths in reports
1 parent e4eec34 commit ceef0af

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/_pytest/reports.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from _pytest._code.code import ReprTraceback
1212
from _pytest._code.code import TerminalRepr
1313
from _pytest.outcomes import skip
14+
from _pytest.pathlib import Path
1415

1516

1617
def getslaveinfoline(node):
@@ -189,7 +190,7 @@ def disassembled_report(rep):
189190
else:
190191
d["longrepr"] = self.longrepr
191192
for name in d:
192-
if isinstance(d[name], py.path.local):
193+
if isinstance(d[name], (py.path.local, Path)):
193194
d[name] = str(d[name])
194195
elif name == "result":
195196
d[name] = None # for now

testing/test_reports.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from _pytest.pathlib import Path
12
from _pytest.reports import CollectReport
23
from _pytest.reports import TestReport
34

@@ -11,7 +12,6 @@ def test_xdist_longrepr_to_str_issue_241(self, testdir):
1112
"""
1213
testdir.makepyfile(
1314
"""
14-
import os
1515
def test_a(): assert False
1616
def test_b(): pass
1717
"""
@@ -35,8 +35,8 @@ def test_xdist_report_longrepr_reprcrash_130(self, testdir):
3535
"""
3636
reprec = testdir.inline_runsource(
3737
"""
38-
import py
39-
def test_fail(): assert False, 'Expected Message'
38+
def test_fail():
39+
assert False, 'Expected Message'
4040
"""
4141
)
4242
reports = reprec.getreports("pytest_runtest_logreport")
@@ -201,6 +201,24 @@ def test_extended_report_deserialization(self, testdir):
201201
if rep.failed:
202202
assert newrep.longrepr == str(rep.longrepr)
203203

204+
def test_paths_support(self, testdir):
205+
"""Report attributes which are py.path or pathlib objects should become strings."""
206+
testdir.makepyfile(
207+
"""
208+
def test_a():
209+
assert False
210+
"""
211+
)
212+
reprec = testdir.inline_run()
213+
reports = reprec.getreports("pytest_runtest_logreport")
214+
assert len(reports) == 3
215+
test_a_call = reports[1]
216+
test_a_call.path1 = testdir.tmpdir
217+
test_a_call.path2 = Path(testdir.tmpdir)
218+
data = test_a_call._to_json()
219+
assert data["path1"] == str(testdir.tmpdir)
220+
assert data["path2"] == str(testdir.tmpdir)
221+
204222

205223
class TestHooks:
206224
"""Test that the hooks are working correctly for plugins"""

0 commit comments

Comments
 (0)