From 3267f64724085c8f4be4c8d6eee6726cd7b20c14 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 13 Mar 2020 09:30:04 -0300 Subject: [PATCH] Merge pull request #6910 from nicoddemus/resultlog-logreport Handle unknown stats in pytest_report_teststatus hook --- changelog/6910.bugfix.rst | 1 + src/_pytest/resultlog.py | 4 ++-- testing/test_resultlog.py | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 changelog/6910.bugfix.rst diff --git a/changelog/6910.bugfix.rst b/changelog/6910.bugfix.rst new file mode 100644 index 00000000000..713824998d1 --- /dev/null +++ b/changelog/6910.bugfix.rst @@ -0,0 +1 @@ +Fix crash when plugins return an unknown stats while using the ``--reportlog`` option. diff --git a/src/_pytest/resultlog.py b/src/_pytest/resultlog.py index 6269c16f2be..3cfa9e0e96a 100644 --- a/src/_pytest/resultlog.py +++ b/src/_pytest/resultlog.py @@ -77,10 +77,10 @@ def pytest_runtest_logreport(self, report): longrepr = "" elif report.passed: longrepr = "" - elif report.failed: - longrepr = str(report.longrepr) elif report.skipped: longrepr = str(report.longrepr[2]) + else: + longrepr = str(report.longrepr) self.log_outcome(report, code, longrepr) def pytest_collectreport(self, report): diff --git a/testing/test_resultlog.py b/testing/test_resultlog.py index e0a02de8029..bad575e3d13 100644 --- a/testing/test_resultlog.py +++ b/testing/test_resultlog.py @@ -193,6 +193,42 @@ def test_no_resultlog_on_slaves(testdir): assert resultlog_key not in config._store +def test_unknown_teststatus(testdir): + """Ensure resultlog correctly handles unknown status from pytest_report_teststatus + + Inspired on pytest-rerunfailures. + """ + testdir.makepyfile( + """ + def test(): + assert 0 + """ + ) + testdir.makeconftest( + """ + import pytest + + def pytest_report_teststatus(report): + if report.outcome == 'rerun': + return "rerun", "r", "RERUN" + + @pytest.hookimpl(hookwrapper=True) + def pytest_runtest_makereport(): + res = yield + report = res.get_result() + if report.when == "call": + report.outcome = 'rerun' + """ + ) + result = testdir.runpytest("--resultlog=result.log") + result.stdout.fnmatch_lines( + ["test_unknown_teststatus.py r *[[]100%[]]", "* 1 rerun *"] + ) + + lines = testdir.tmpdir.join("result.log").readlines(cr=0) + assert lines[0] == "r test_unknown_teststatus.py::test" + + def test_failure_issue380(testdir): testdir.makeconftest( """