From ceef0af1aea4c8db3b8670a2ff4f127a56028bb4 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira <nicoddemus@gmail.com> Date: Mon, 25 Mar 2019 19:19:59 -0300 Subject: [PATCH] Improve coverage for to_json() with paths in reports --- src/_pytest/reports.py | 3 ++- testing/test_reports.py | 24 +++++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/_pytest/reports.py b/src/_pytest/reports.py index 74faa3f8c..8ab42478f 100644 --- a/src/_pytest/reports.py +++ b/src/_pytest/reports.py @@ -11,6 +11,7 @@ from _pytest._code.code import ReprLocals from _pytest._code.code import ReprTraceback from _pytest._code.code import TerminalRepr from _pytest.outcomes import skip +from _pytest.pathlib import Path def getslaveinfoline(node): @@ -189,7 +190,7 @@ class BaseReport(object): else: d["longrepr"] = self.longrepr for name in d: - if isinstance(d[name], py.path.local): + if isinstance(d[name], (py.path.local, Path)): d[name] = str(d[name]) elif name == "result": d[name] = None # for now diff --git a/testing/test_reports.py b/testing/test_reports.py index f37ead893..2f9162e10 100644 --- a/testing/test_reports.py +++ b/testing/test_reports.py @@ -1,3 +1,4 @@ +from _pytest.pathlib import Path from _pytest.reports import CollectReport from _pytest.reports import TestReport @@ -11,7 +12,6 @@ class TestReportSerialization(object): """ testdir.makepyfile( """ - import os def test_a(): assert False def test_b(): pass """ @@ -35,8 +35,8 @@ class TestReportSerialization(object): """ reprec = testdir.inline_runsource( """ - import py - def test_fail(): assert False, 'Expected Message' + def test_fail(): + assert False, 'Expected Message' """ ) reports = reprec.getreports("pytest_runtest_logreport") @@ -201,6 +201,24 @@ class TestReportSerialization(object): if rep.failed: assert newrep.longrepr == str(rep.longrepr) + def test_paths_support(self, testdir): + """Report attributes which are py.path or pathlib objects should become strings.""" + testdir.makepyfile( + """ + def test_a(): + assert False + """ + ) + reprec = testdir.inline_run() + reports = reprec.getreports("pytest_runtest_logreport") + assert len(reports) == 3 + test_a_call = reports[1] + test_a_call.path1 = testdir.tmpdir + test_a_call.path2 = Path(testdir.tmpdir) + data = test_a_call._to_json() + assert data["path1"] == str(testdir.tmpdir) + assert data["path2"] == str(testdir.tmpdir) + class TestHooks: """Test that the hooks are working correctly for plugins"""