diff --git a/src/_pytest/reports.py b/src/_pytest/reports.py index cd7a0b57f..679d72c9d 100644 --- a/src/_pytest/reports.py +++ b/src/_pytest/reports.py @@ -422,4 +422,6 @@ def pytest_report_unserialize(data): return TestReport._from_json(data) elif data["_report_type"] == "CollectReport": return CollectReport._from_json(data) - assert "Unknown report_type unserialize data: {}".format(data["_report_type"]) + assert False, "Unknown report_type unserialize data: {}".format( + data["_report_type"] + ) diff --git a/testing/test_reports.py b/testing/test_reports.py index 879a9098d..22d5fce34 100644 --- a/testing/test_reports.py +++ b/testing/test_reports.py @@ -249,7 +249,6 @@ class TestHooks: def test_test_report(self, testdir, pytestconfig): testdir.makepyfile( """ - import os def test_a(): assert False def test_b(): pass """ @@ -272,7 +271,6 @@ class TestHooks: def test_collect_report(self, testdir, pytestconfig): testdir.makepyfile( """ - import os def test_a(): assert False def test_b(): pass """ @@ -291,3 +289,25 @@ class TestHooks: assert new_rep.nodeid == rep.nodeid assert new_rep.when == "collect" assert new_rep.outcome == rep.outcome + + @pytest.mark.parametrize( + "hook_name", ["pytest_runtest_logreport", "pytest_collectreport"] + ) + def test_invalid_report_types(self, testdir, pytestconfig, hook_name): + testdir.makepyfile( + """ + def test_a(): pass + """ + ) + reprec = testdir.inline_run() + reports = reprec.getreports(hook_name) + assert reports + rep = reports[0] + data = pytestconfig.hook.pytest_report_serialize( + config=pytestconfig, report=rep + ) + data["_report_type"] = "Unknown" + with pytest.raises(AssertionError): + _ = pytestconfig.hook.pytest_report_unserialize( + config=pytestconfig, data=data + )