2024-01-28 21:12:42 +08:00
|
|
|
# mypy: allow-untyped-defs
|
2020-08-01 15:28:39 +08:00
|
|
|
from typing import Sequence
|
|
|
|
from typing import Union
|
2020-01-14 21:51:44 +08:00
|
|
|
|
2019-03-26 06:26:40 +08:00
|
|
|
import pytest
|
2019-08-27 00:21:53 +08:00
|
|
|
from _pytest._code.code import ExceptionChainRepr
|
2020-08-01 15:28:39 +08:00
|
|
|
from _pytest._code.code import ExceptionRepr
|
|
|
|
from _pytest.config import Config
|
2020-12-15 19:02:32 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2023-02-11 04:52:54 +08:00
|
|
|
from _pytest.python_api import approx
|
2019-03-21 07:24:51 +08:00
|
|
|
from _pytest.reports import CollectReport
|
|
|
|
from _pytest.reports import TestReport
|
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestReportSerialization:
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_xdist_longrepr_to_str_issue_241(self, pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Regarding issue pytest-xdist#241.
|
2019-03-26 06:12:02 +08:00
|
|
|
|
|
|
|
This test came originally from test_remote.py in xdist (ca03269).
|
|
|
|
"""
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2019-03-21 07:24:51 +08:00
|
|
|
"""
|
|
|
|
def test_a(): assert False
|
|
|
|
def test_b(): pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
reprec = pytester.inline_run()
|
2019-03-21 07:24:51 +08:00
|
|
|
reports = reprec.getreports("pytest_runtest_logreport")
|
|
|
|
assert len(reports) == 6
|
|
|
|
test_a_call = reports[1]
|
|
|
|
assert test_a_call.when == "call"
|
|
|
|
assert test_a_call.outcome == "failed"
|
|
|
|
assert test_a_call._to_json()["longrepr"]["reprtraceback"]["style"] == "long"
|
|
|
|
test_b_call = reports[4]
|
|
|
|
assert test_b_call.when == "call"
|
|
|
|
assert test_b_call.outcome == "passed"
|
|
|
|
assert test_b_call._to_json()["longrepr"] is None
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_xdist_report_longrepr_reprcrash_130(self, pytester: Pytester) -> None:
|
2019-03-26 06:12:02 +08:00
|
|
|
"""Regarding issue pytest-xdist#130
|
|
|
|
|
|
|
|
This test came originally from test_remote.py in xdist (ca03269).
|
|
|
|
"""
|
2020-12-15 19:02:32 +08:00
|
|
|
reprec = pytester.inline_runsource(
|
2019-03-21 07:24:51 +08:00
|
|
|
"""
|
2019-03-26 06:19:59 +08:00
|
|
|
def test_fail():
|
|
|
|
assert False, 'Expected Message'
|
2019-03-21 07:24:51 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
reports = reprec.getreports("pytest_runtest_logreport")
|
|
|
|
assert len(reports) == 3
|
|
|
|
rep = reports[1]
|
2019-06-03 06:32:00 +08:00
|
|
|
added_section = ("Failure Metadata", "metadata metadata", "*")
|
2020-08-01 15:28:39 +08:00
|
|
|
assert isinstance(rep.longrepr, ExceptionRepr)
|
2019-03-21 07:24:51 +08:00
|
|
|
rep.longrepr.sections.append(added_section)
|
|
|
|
d = rep._to_json()
|
|
|
|
a = TestReport._from_json(d)
|
2020-08-01 15:28:39 +08:00
|
|
|
assert isinstance(a.longrepr, ExceptionRepr)
|
2019-03-21 07:24:51 +08:00
|
|
|
# Check assembled == rep
|
|
|
|
assert a.__dict__.keys() == rep.__dict__.keys()
|
|
|
|
for key in rep.__dict__.keys():
|
|
|
|
if key != "longrepr":
|
|
|
|
assert getattr(a, key) == getattr(rep, key)
|
2020-08-01 15:28:39 +08:00
|
|
|
assert rep.longrepr.reprcrash is not None
|
|
|
|
assert a.longrepr.reprcrash is not None
|
2019-03-21 07:24:51 +08:00
|
|
|
assert rep.longrepr.reprcrash.lineno == a.longrepr.reprcrash.lineno
|
|
|
|
assert rep.longrepr.reprcrash.message == a.longrepr.reprcrash.message
|
|
|
|
assert rep.longrepr.reprcrash.path == a.longrepr.reprcrash.path
|
|
|
|
assert rep.longrepr.reprtraceback.entrysep == a.longrepr.reprtraceback.entrysep
|
|
|
|
assert (
|
|
|
|
rep.longrepr.reprtraceback.extraline == a.longrepr.reprtraceback.extraline
|
|
|
|
)
|
|
|
|
assert rep.longrepr.reprtraceback.style == a.longrepr.reprtraceback.style
|
|
|
|
assert rep.longrepr.sections == a.longrepr.sections
|
|
|
|
# Missing section attribute PR171
|
|
|
|
assert added_section in a.longrepr.sections
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_reprentries_serialization_170(self, pytester: Pytester) -> None:
|
2019-03-26 06:12:02 +08:00
|
|
|
"""Regarding issue pytest-xdist#170
|
|
|
|
|
|
|
|
This test came originally from test_remote.py in xdist (ca03269).
|
|
|
|
"""
|
2019-03-21 07:24:51 +08:00
|
|
|
from _pytest._code.code import ReprEntry
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
reprec = pytester.inline_runsource(
|
2019-03-21 07:24:51 +08:00
|
|
|
"""
|
|
|
|
def test_repr_entry():
|
|
|
|
x = 0
|
|
|
|
assert x
|
|
|
|
""",
|
|
|
|
"--showlocals",
|
|
|
|
)
|
|
|
|
reports = reprec.getreports("pytest_runtest_logreport")
|
|
|
|
assert len(reports) == 3
|
|
|
|
rep = reports[1]
|
2020-08-01 15:28:39 +08:00
|
|
|
assert isinstance(rep.longrepr, ExceptionRepr)
|
2019-03-21 07:24:51 +08:00
|
|
|
d = rep._to_json()
|
|
|
|
a = TestReport._from_json(d)
|
2020-08-01 15:28:39 +08:00
|
|
|
assert isinstance(a.longrepr, ExceptionRepr)
|
2019-03-21 07:24:51 +08:00
|
|
|
|
|
|
|
rep_entries = rep.longrepr.reprtraceback.reprentries
|
|
|
|
a_entries = a.longrepr.reprtraceback.reprentries
|
|
|
|
for i in range(len(a_entries)):
|
2020-08-01 15:28:39 +08:00
|
|
|
rep_entry = rep_entries[i]
|
|
|
|
assert isinstance(rep_entry, ReprEntry)
|
|
|
|
assert rep_entry.reprfileloc is not None
|
|
|
|
assert rep_entry.reprfuncargs is not None
|
|
|
|
assert rep_entry.reprlocals is not None
|
|
|
|
|
|
|
|
a_entry = a_entries[i]
|
|
|
|
assert isinstance(a_entry, ReprEntry)
|
|
|
|
assert a_entry.reprfileloc is not None
|
|
|
|
assert a_entry.reprfuncargs is not None
|
|
|
|
assert a_entry.reprlocals is not None
|
|
|
|
|
|
|
|
assert rep_entry.lines == a_entry.lines
|
|
|
|
assert rep_entry.reprfileloc.lineno == a_entry.reprfileloc.lineno
|
|
|
|
assert rep_entry.reprfileloc.message == a_entry.reprfileloc.message
|
|
|
|
assert rep_entry.reprfileloc.path == a_entry.reprfileloc.path
|
|
|
|
assert rep_entry.reprfuncargs.args == a_entry.reprfuncargs.args
|
|
|
|
assert rep_entry.reprlocals.lines == a_entry.reprlocals.lines
|
|
|
|
assert rep_entry.style == a_entry.style
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_reprentries_serialization_196(self, pytester: Pytester) -> None:
|
2019-03-26 06:12:02 +08:00
|
|
|
"""Regarding issue pytest-xdist#196
|
|
|
|
|
|
|
|
This test came originally from test_remote.py in xdist (ca03269).
|
|
|
|
"""
|
2019-03-21 07:24:51 +08:00
|
|
|
from _pytest._code.code import ReprEntryNative
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
reprec = pytester.inline_runsource(
|
2019-03-21 07:24:51 +08:00
|
|
|
"""
|
|
|
|
def test_repr_entry_native():
|
|
|
|
x = 0
|
|
|
|
assert x
|
|
|
|
""",
|
|
|
|
"--tb=native",
|
|
|
|
)
|
|
|
|
reports = reprec.getreports("pytest_runtest_logreport")
|
|
|
|
assert len(reports) == 3
|
|
|
|
rep = reports[1]
|
2020-08-01 15:28:39 +08:00
|
|
|
assert isinstance(rep.longrepr, ExceptionRepr)
|
2019-03-21 07:24:51 +08:00
|
|
|
d = rep._to_json()
|
|
|
|
a = TestReport._from_json(d)
|
2020-08-01 15:28:39 +08:00
|
|
|
assert isinstance(a.longrepr, ExceptionRepr)
|
2019-03-21 07:24:51 +08:00
|
|
|
|
|
|
|
rep_entries = rep.longrepr.reprtraceback.reprentries
|
|
|
|
a_entries = a.longrepr.reprtraceback.reprentries
|
|
|
|
for i in range(len(a_entries)):
|
|
|
|
assert isinstance(rep_entries[i], ReprEntryNative)
|
|
|
|
assert rep_entries[i].lines == a_entries[i].lines
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_itemreport_outcomes(self, pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
# This test came originally from test_remote.py in xdist (ca03269).
|
2020-12-15 19:02:32 +08:00
|
|
|
reprec = pytester.inline_runsource(
|
2019-03-21 07:24:51 +08:00
|
|
|
"""
|
2019-10-26 21:00:04 +08:00
|
|
|
import pytest
|
2019-03-21 07:24:51 +08:00
|
|
|
def test_pass(): pass
|
|
|
|
def test_fail(): 0/0
|
2019-10-26 21:00:04 +08:00
|
|
|
@pytest.mark.skipif("True")
|
2019-03-21 07:24:51 +08:00
|
|
|
def test_skip(): pass
|
|
|
|
def test_skip_imperative():
|
2019-10-26 21:00:04 +08:00
|
|
|
pytest.skip("hello")
|
|
|
|
@pytest.mark.xfail("True")
|
2019-03-21 07:24:51 +08:00
|
|
|
def test_xfail(): 0/0
|
|
|
|
def test_xfail_imperative():
|
2019-10-26 21:00:04 +08:00
|
|
|
pytest.xfail("hello")
|
2019-03-21 07:24:51 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
reports = reprec.getreports("pytest_runtest_logreport")
|
|
|
|
assert len(reports) == 17 # with setup/teardown "passed" reports
|
|
|
|
for rep in reports:
|
|
|
|
d = rep._to_json()
|
|
|
|
newrep = TestReport._from_json(d)
|
|
|
|
assert newrep.passed == rep.passed
|
|
|
|
assert newrep.failed == rep.failed
|
|
|
|
assert newrep.skipped == rep.skipped
|
|
|
|
if newrep.skipped and not hasattr(newrep, "wasxfail"):
|
2020-08-01 15:28:39 +08:00
|
|
|
assert isinstance(newrep.longrepr, tuple)
|
2019-03-21 07:24:51 +08:00
|
|
|
assert len(newrep.longrepr) == 3
|
|
|
|
assert newrep.outcome == rep.outcome
|
|
|
|
assert newrep.when == rep.when
|
|
|
|
assert newrep.keywords == rep.keywords
|
|
|
|
if rep.failed:
|
|
|
|
assert newrep.longreprtext == rep.longreprtext
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_collectreport_passed(self, pytester: Pytester) -> None:
|
2019-03-26 06:12:02 +08:00
|
|
|
"""This test came originally from test_remote.py in xdist (ca03269)."""
|
2020-12-15 19:02:32 +08:00
|
|
|
reprec = pytester.inline_runsource("def test_func(): pass")
|
2019-03-21 07:24:51 +08:00
|
|
|
reports = reprec.getreports("pytest_collectreport")
|
|
|
|
for rep in reports:
|
|
|
|
d = rep._to_json()
|
|
|
|
newrep = CollectReport._from_json(d)
|
|
|
|
assert newrep.passed == rep.passed
|
|
|
|
assert newrep.failed == rep.failed
|
|
|
|
assert newrep.skipped == rep.skipped
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_collectreport_fail(self, pytester: Pytester) -> None:
|
2019-03-26 06:12:02 +08:00
|
|
|
"""This test came originally from test_remote.py in xdist (ca03269)."""
|
2020-12-15 19:02:32 +08:00
|
|
|
reprec = pytester.inline_runsource("qwe abc")
|
2019-03-21 07:24:51 +08:00
|
|
|
reports = reprec.getreports("pytest_collectreport")
|
|
|
|
assert reports
|
|
|
|
for rep in reports:
|
|
|
|
d = rep._to_json()
|
|
|
|
newrep = CollectReport._from_json(d)
|
|
|
|
assert newrep.passed == rep.passed
|
|
|
|
assert newrep.failed == rep.failed
|
|
|
|
assert newrep.skipped == rep.skipped
|
|
|
|
if rep.failed:
|
|
|
|
assert newrep.longrepr == str(rep.longrepr)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_extended_report_deserialization(self, pytester: Pytester) -> None:
|
2019-03-26 06:12:02 +08:00
|
|
|
"""This test came originally from test_remote.py in xdist (ca03269)."""
|
2020-12-15 19:02:32 +08:00
|
|
|
reprec = pytester.inline_runsource("qwe abc")
|
2019-03-21 07:24:51 +08:00
|
|
|
reports = reprec.getreports("pytest_collectreport")
|
|
|
|
assert reports
|
|
|
|
for rep in reports:
|
2020-08-01 15:28:39 +08:00
|
|
|
rep.extra = True # type: ignore[attr-defined]
|
2019-03-21 07:24:51 +08:00
|
|
|
d = rep._to_json()
|
|
|
|
newrep = CollectReport._from_json(d)
|
|
|
|
assert newrep.extra
|
|
|
|
assert newrep.passed == rep.passed
|
|
|
|
assert newrep.failed == rep.failed
|
|
|
|
assert newrep.skipped == rep.skipped
|
|
|
|
if rep.failed:
|
|
|
|
assert newrep.longrepr == str(rep.longrepr)
|
2019-03-21 07:45:15 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_paths_support(self, pytester: Pytester) -> None:
|
2021-03-15 23:20:55 +08:00
|
|
|
"""Report attributes which are path-like should become strings."""
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2019-03-26 06:19:59 +08:00
|
|
|
"""
|
|
|
|
def test_a():
|
|
|
|
assert False
|
|
|
|
"""
|
|
|
|
)
|
2021-03-15 23:20:55 +08:00
|
|
|
|
|
|
|
class MyPathLike:
|
|
|
|
def __init__(self, path: str) -> None:
|
|
|
|
self.path = path
|
|
|
|
|
|
|
|
def __fspath__(self) -> str:
|
|
|
|
return self.path
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
reprec = pytester.inline_run()
|
2019-03-26 06:19:59 +08:00
|
|
|
reports = reprec.getreports("pytest_runtest_logreport")
|
|
|
|
assert len(reports) == 3
|
|
|
|
test_a_call = reports[1]
|
2021-03-15 23:20:55 +08:00
|
|
|
test_a_call.path1 = MyPathLike(str(pytester.path)) # type: ignore[attr-defined]
|
2020-12-15 19:02:32 +08:00
|
|
|
test_a_call.path2 = pytester.path # type: ignore[attr-defined]
|
2019-03-26 06:19:59 +08:00
|
|
|
data = test_a_call._to_json()
|
2020-12-15 19:02:32 +08:00
|
|
|
assert data["path1"] == str(pytester.path)
|
|
|
|
assert data["path2"] == str(pytester.path)
|
2019-03-26 06:19:59 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_deserialization_failure(self, pytester: Pytester) -> None:
|
2019-08-27 00:21:53 +08:00
|
|
|
"""Check handling of failure during deserialization of report types."""
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2019-03-26 06:26:40 +08:00
|
|
|
"""
|
|
|
|
def test_a():
|
|
|
|
assert False
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
reprec = pytester.inline_run()
|
2019-03-26 06:26:40 +08:00
|
|
|
reports = reprec.getreports("pytest_runtest_logreport")
|
|
|
|
assert len(reports) == 3
|
|
|
|
test_a_call = reports[1]
|
|
|
|
data = test_a_call._to_json()
|
|
|
|
entry = data["longrepr"]["reprtraceback"]["reprentries"][0]
|
|
|
|
assert entry["type"] == "ReprEntry"
|
|
|
|
|
|
|
|
entry["type"] = "Unknown"
|
|
|
|
with pytest.raises(
|
|
|
|
RuntimeError, match="INTERNALERROR: Unknown entry type returned: Unknown"
|
|
|
|
):
|
|
|
|
TestReport._from_json(data)
|
|
|
|
|
2019-08-27 00:21:53 +08:00
|
|
|
@pytest.mark.parametrize("report_class", [TestReport, CollectReport])
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_chained_exceptions(
|
|
|
|
self, pytester: Pytester, tw_mock, report_class
|
|
|
|
) -> None:
|
2019-08-27 00:21:53 +08:00
|
|
|
"""Check serialization/deserialization of report objects containing chained exceptions (#5786)"""
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2019-08-27 00:21:53 +08:00
|
|
|
"""
|
|
|
|
def foo():
|
|
|
|
raise ValueError('value error')
|
|
|
|
def test_a():
|
|
|
|
try:
|
|
|
|
foo()
|
|
|
|
except ValueError as e:
|
|
|
|
raise RuntimeError('runtime error') from e
|
|
|
|
if {error_during_import}:
|
|
|
|
test_a()
|
|
|
|
""".format(
|
|
|
|
error_during_import=report_class is CollectReport
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
reprec = pytester.inline_run()
|
2019-08-27 00:21:53 +08:00
|
|
|
if report_class is TestReport:
|
2020-10-06 09:13:05 +08:00
|
|
|
reports: Union[
|
|
|
|
Sequence[TestReport], Sequence[CollectReport]
|
|
|
|
] = reprec.getreports("pytest_runtest_logreport")
|
2019-08-27 00:21:53 +08:00
|
|
|
# we have 3 reports: setup/call/teardown
|
|
|
|
assert len(reports) == 3
|
|
|
|
# get the call report
|
|
|
|
report = reports[1]
|
|
|
|
else:
|
|
|
|
assert report_class is CollectReport
|
2023-06-02 21:03:39 +08:00
|
|
|
# three collection reports: session, test file, directory
|
2019-08-27 00:21:53 +08:00
|
|
|
reports = reprec.getreports("pytest_collectreport")
|
2023-06-02 21:03:39 +08:00
|
|
|
assert len(reports) == 3
|
2019-08-27 00:21:53 +08:00
|
|
|
report = reports[1]
|
|
|
|
|
2020-08-01 15:28:39 +08:00
|
|
|
def check_longrepr(longrepr: ExceptionChainRepr) -> None:
|
2019-08-27 00:21:53 +08:00
|
|
|
"""Check the attributes of the given longrepr object according to the test file.
|
|
|
|
|
|
|
|
We can get away with testing both CollectReport and TestReport with this function because
|
|
|
|
the longrepr objects are very similar.
|
|
|
|
"""
|
|
|
|
assert isinstance(longrepr, ExceptionChainRepr)
|
|
|
|
assert longrepr.sections == [("title", "contents", "=")]
|
|
|
|
assert len(longrepr.chain) == 2
|
|
|
|
entry1, entry2 = longrepr.chain
|
|
|
|
tb1, fileloc1, desc1 = entry1
|
|
|
|
tb2, fileloc2, desc2 = entry2
|
|
|
|
|
|
|
|
assert "ValueError('value error')" in str(tb1)
|
|
|
|
assert "RuntimeError('runtime error')" in str(tb2)
|
|
|
|
|
|
|
|
assert (
|
|
|
|
desc1
|
|
|
|
== "The above exception was the direct cause of the following exception:"
|
|
|
|
)
|
|
|
|
assert desc2 is None
|
|
|
|
|
|
|
|
assert report.failed
|
|
|
|
assert len(report.sections) == 0
|
2020-08-01 15:28:39 +08:00
|
|
|
assert isinstance(report.longrepr, ExceptionChainRepr)
|
2019-08-27 00:21:53 +08:00
|
|
|
report.longrepr.addsection("title", "contents", "=")
|
|
|
|
check_longrepr(report.longrepr)
|
|
|
|
|
|
|
|
data = report._to_json()
|
|
|
|
loaded_report = report_class._from_json(data)
|
2020-01-07 08:16:23 +08:00
|
|
|
|
|
|
|
assert loaded_report.failed
|
2019-08-27 00:21:53 +08:00
|
|
|
check_longrepr(loaded_report.longrepr)
|
|
|
|
|
|
|
|
# make sure we don't blow up on ``toterminal`` call; we don't test the actual output because it is very
|
|
|
|
# brittle and hard to maintain, but we can assume it is correct because ``toterminal`` is already tested
|
|
|
|
# elsewhere and we do check the contents of the longrepr object after loading it.
|
|
|
|
loaded_report.longrepr.toterminal(tw_mock)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_chained_exceptions_no_reprcrash(self, pytester: Pytester, tw_mock) -> None:
|
2020-01-07 08:16:23 +08:00
|
|
|
"""Regression test for tracebacks without a reprcrash (#5971)
|
|
|
|
|
|
|
|
This happens notably on exceptions raised by multiprocess.pool: the exception transfer
|
2020-01-07 23:45:18 +08:00
|
|
|
from subprocess to main process creates an artificial exception, which ExceptionInfo
|
2020-01-07 08:16:23 +08:00
|
|
|
can't obtain the ReprFileLocation from.
|
|
|
|
"""
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2020-01-07 08:16:23 +08:00
|
|
|
"""
|
2020-10-19 15:02:36 +08:00
|
|
|
from concurrent.futures import ProcessPoolExecutor
|
2020-01-07 23:45:18 +08:00
|
|
|
|
2020-10-19 15:02:36 +08:00
|
|
|
def func():
|
|
|
|
raise ValueError('value error')
|
2020-01-14 21:51:44 +08:00
|
|
|
|
2020-10-19 15:02:36 +08:00
|
|
|
def test_a():
|
|
|
|
with ProcessPoolExecutor() as p:
|
|
|
|
p.submit(func).result()
|
|
|
|
"""
|
|
|
|
)
|
2020-01-07 08:16:23 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.syspathinsert()
|
|
|
|
reprec = pytester.inline_run()
|
2020-01-07 08:16:23 +08:00
|
|
|
|
|
|
|
reports = reprec.getreports("pytest_runtest_logreport")
|
|
|
|
|
2020-08-01 15:28:39 +08:00
|
|
|
def check_longrepr(longrepr: object) -> None:
|
2020-01-07 08:16:23 +08:00
|
|
|
assert isinstance(longrepr, ExceptionChainRepr)
|
|
|
|
assert len(longrepr.chain) == 2
|
|
|
|
entry1, entry2 = longrepr.chain
|
|
|
|
tb1, fileloc1, desc1 = entry1
|
|
|
|
tb2, fileloc2, desc2 = entry2
|
|
|
|
|
2020-01-07 23:45:18 +08:00
|
|
|
assert "RemoteTraceback" in str(tb1)
|
|
|
|
assert "ValueError: value error" in str(tb2)
|
2020-01-07 08:16:23 +08:00
|
|
|
|
|
|
|
assert fileloc1 is None
|
2020-05-01 19:40:17 +08:00
|
|
|
assert fileloc2 is not None
|
2020-01-07 08:16:23 +08:00
|
|
|
assert fileloc2.message == "ValueError: value error"
|
|
|
|
|
|
|
|
# 3 reports: setup/call/teardown: get the call report
|
|
|
|
assert len(reports) == 3
|
|
|
|
report = reports[1]
|
|
|
|
|
|
|
|
assert report.failed
|
|
|
|
check_longrepr(report.longrepr)
|
|
|
|
|
|
|
|
data = report._to_json()
|
|
|
|
loaded_report = TestReport._from_json(data)
|
|
|
|
|
|
|
|
assert loaded_report.failed
|
|
|
|
check_longrepr(loaded_report.longrepr)
|
|
|
|
|
|
|
|
# for same reasons as previous test, ensure we don't blow up here
|
2020-05-01 19:40:17 +08:00
|
|
|
assert loaded_report.longrepr is not None
|
2020-08-01 15:28:39 +08:00
|
|
|
assert isinstance(loaded_report.longrepr, ExceptionChainRepr)
|
2020-01-07 08:16:23 +08:00
|
|
|
loaded_report.longrepr.toterminal(tw_mock)
|
|
|
|
|
2020-08-01 15:28:39 +08:00
|
|
|
def test_report_prevent_ConftestImportFailure_hiding_exception(
|
2020-12-15 19:02:32 +08:00
|
|
|
self, pytester: Pytester
|
2020-08-01 15:28:39 +08:00
|
|
|
) -> None:
|
2020-12-15 19:02:32 +08:00
|
|
|
sub_dir = pytester.path.joinpath("ns")
|
|
|
|
sub_dir.mkdir()
|
2023-06-20 19:55:40 +08:00
|
|
|
sub_dir.joinpath("conftest.py").write_text("import unknown", encoding="utf-8")
|
2020-05-25 19:53:56 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest_subprocess(".")
|
2020-05-27 15:27:13 +08:00
|
|
|
result.stdout.fnmatch_lines(["E *Error: No module named 'unknown'"])
|
2020-05-26 16:31:53 +08:00
|
|
|
result.stdout.no_fnmatch_line("ERROR - *ConftestImportFailure*")
|
2020-05-25 19:53:56 +08:00
|
|
|
|
2023-02-11 04:52:54 +08:00
|
|
|
def test_report_timestamps_match_duration(self, pytester: Pytester, mock_timing):
|
|
|
|
reprec = pytester.inline_runsource(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
from _pytest import timing
|
|
|
|
@pytest.fixture
|
|
|
|
def fixture_():
|
|
|
|
timing.sleep(5)
|
|
|
|
yield
|
|
|
|
timing.sleep(5)
|
|
|
|
def test_1(fixture_): timing.sleep(10)
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
reports = reprec.getreports("pytest_runtest_logreport")
|
|
|
|
assert len(reports) == 3
|
|
|
|
for report in reports:
|
|
|
|
data = report._to_json()
|
|
|
|
loaded_report = TestReport._from_json(data)
|
|
|
|
assert loaded_report.stop - loaded_report.start == approx(report.duration)
|
|
|
|
|
2019-03-21 07:45:15 +08:00
|
|
|
|
|
|
|
class TestHooks:
|
|
|
|
"""Test that the hooks are working correctly for plugins"""
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_test_report(self, pytester: Pytester, pytestconfig: Config) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-03-21 07:45:15 +08:00
|
|
|
"""
|
|
|
|
def test_a(): assert False
|
|
|
|
def test_b(): pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
reprec = pytester.inline_run()
|
2019-03-21 07:45:15 +08:00
|
|
|
reports = reprec.getreports("pytest_runtest_logreport")
|
|
|
|
assert len(reports) == 6
|
|
|
|
for rep in reports:
|
2019-03-29 00:41:56 +08:00
|
|
|
data = pytestconfig.hook.pytest_report_to_serializable(
|
2019-03-21 07:45:15 +08:00
|
|
|
config=pytestconfig, report=rep
|
|
|
|
)
|
2019-10-17 06:24:41 +08:00
|
|
|
assert data["$report_type"] == "TestReport"
|
2019-03-29 00:41:56 +08:00
|
|
|
new_rep = pytestconfig.hook.pytest_report_from_serializable(
|
2019-03-21 07:45:15 +08:00
|
|
|
config=pytestconfig, data=data
|
|
|
|
)
|
|
|
|
assert new_rep.nodeid == rep.nodeid
|
|
|
|
assert new_rep.when == rep.when
|
|
|
|
assert new_rep.outcome == rep.outcome
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_collect_report(self, pytester: Pytester, pytestconfig: Config) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-03-21 07:45:15 +08:00
|
|
|
"""
|
|
|
|
def test_a(): assert False
|
|
|
|
def test_b(): pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
reprec = pytester.inline_run()
|
2019-03-21 07:45:15 +08:00
|
|
|
reports = reprec.getreports("pytest_collectreport")
|
2023-06-02 21:03:39 +08:00
|
|
|
assert len(reports) == 3
|
2019-03-21 07:45:15 +08:00
|
|
|
for rep in reports:
|
2019-03-29 00:41:56 +08:00
|
|
|
data = pytestconfig.hook.pytest_report_to_serializable(
|
2019-03-21 07:45:15 +08:00
|
|
|
config=pytestconfig, report=rep
|
|
|
|
)
|
2019-10-17 06:24:41 +08:00
|
|
|
assert data["$report_type"] == "CollectReport"
|
2019-03-29 00:41:56 +08:00
|
|
|
new_rep = pytestconfig.hook.pytest_report_from_serializable(
|
2019-03-21 07:45:15 +08:00
|
|
|
config=pytestconfig, data=data
|
|
|
|
)
|
|
|
|
assert new_rep.nodeid == rep.nodeid
|
|
|
|
assert new_rep.when == "collect"
|
|
|
|
assert new_rep.outcome == rep.outcome
|
2019-03-26 23:47:31 +08:00
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"hook_name", ["pytest_runtest_logreport", "pytest_collectreport"]
|
|
|
|
)
|
2020-08-01 15:28:39 +08:00
|
|
|
def test_invalid_report_types(
|
2020-12-15 19:02:32 +08:00
|
|
|
self, pytester: Pytester, pytestconfig: Config, hook_name: str
|
2020-08-01 15:28:39 +08:00
|
|
|
) -> None:
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2019-03-26 23:47:31 +08:00
|
|
|
"""
|
|
|
|
def test_a(): pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
reprec = pytester.inline_run()
|
2019-03-26 23:47:31 +08:00
|
|
|
reports = reprec.getreports(hook_name)
|
|
|
|
assert reports
|
|
|
|
rep = reports[0]
|
2019-03-29 00:41:56 +08:00
|
|
|
data = pytestconfig.hook.pytest_report_to_serializable(
|
2019-03-26 23:47:31 +08:00
|
|
|
config=pytestconfig, report=rep
|
|
|
|
)
|
2019-10-17 06:24:41 +08:00
|
|
|
data["$report_type"] = "Unknown"
|
2019-03-26 23:47:31 +08:00
|
|
|
with pytest.raises(AssertionError):
|
2019-03-29 00:41:56 +08:00
|
|
|
_ = pytestconfig.hook.pytest_report_from_serializable(
|
2019-03-26 23:47:31 +08:00
|
|
|
config=pytestconfig, data=data
|
|
|
|
)
|