Skip to content

Commit 9311d82

Browse files
committed
Fix assertion in pytest_report_unserialize
1 parent 2d77018 commit 9311d82

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/_pytest/reports.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,4 +422,6 @@ def pytest_report_unserialize(data):
422422
return TestReport._from_json(data)
423423
elif data["_report_type"] == "CollectReport":
424424
return CollectReport._from_json(data)
425-
assert "Unknown report_type unserialize data: {}".format(data["_report_type"])
425+
assert False, "Unknown report_type unserialize data: {}".format(
426+
data["_report_type"]
427+
)

testing/test_reports.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ class TestHooks:
249249
def test_test_report(self, testdir, pytestconfig):
250250
testdir.makepyfile(
251251
"""
252-
import os
253252
def test_a(): assert False
254253
def test_b(): pass
255254
"""
@@ -272,7 +271,6 @@ def test_b(): pass
272271
def test_collect_report(self, testdir, pytestconfig):
273272
testdir.makepyfile(
274273
"""
275-
import os
276274
def test_a(): assert False
277275
def test_b(): pass
278276
"""
@@ -291,3 +289,25 @@ def test_b(): pass
291289
assert new_rep.nodeid == rep.nodeid
292290
assert new_rep.when == "collect"
293291
assert new_rep.outcome == rep.outcome
292+
293+
@pytest.mark.parametrize(
294+
"hook_name", ["pytest_runtest_logreport", "pytest_collectreport"]
295+
)
296+
def test_invalid_report_types(self, testdir, pytestconfig, hook_name):
297+
testdir.makepyfile(
298+
"""
299+
def test_a(): pass
300+
"""
301+
)
302+
reprec = testdir.inline_run()
303+
reports = reprec.getreports(hook_name)
304+
assert reports
305+
rep = reports[0]
306+
data = pytestconfig.hook.pytest_report_serialize(
307+
config=pytestconfig, report=rep
308+
)
309+
data["_report_type"] = "Unknown"
310+
with pytest.raises(AssertionError):
311+
_ = pytestconfig.hook.pytest_report_unserialize(
312+
config=pytestconfig, data=data
313+
)

0 commit comments

Comments
 (0)