Skip to content

Commit 842814c

Browse files
authored
Merge pull request #9262 from bluetech/export-reports
Export CollectReport and TestReport
2 parents a53abe9 + b0aa870 commit 842814c

File tree

5 files changed

+30
-12
lines changed

5 files changed

+30
-12
lines changed

changelog/7469.feature.rst

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ The newly-exported types are:
1616
- ``pytest.RecordedHookCall`` for the :class:`RecordedHookCall <pytest.HookRecorder>` type returned from :class:`~pytest.HookRecorder`.
1717
- ``pytest.RunResult`` for the :class:`RunResult <pytest.RunResult>` type returned from :class:`~pytest.Pytester`.
1818
- ``pytest.LineMatcher`` for the :class:`LineMatcher <pytest.RunResult>` type used in :class:`~pytest.RunResult` and others.
19+
- ``pytest.TestReport`` for the :class:`TestReport <pytest.TestReport>` type used in various hooks.
20+
- ``pytest.CollectReport`` for the :class:`CollectReport <pytest.CollectReport>` type used in various hooks.
1921

2022
Constructing most of them directly is not supported; they are only meant for use in type annotations.
2123
Doing so will emit a deprecation warning, and may become a hard-error in pytest 8.0.

doc/en/reference/reference.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ Collector
813813
CollectReport
814814
~~~~~~~~~~~~~
815815

816-
.. autoclass:: _pytest.reports.CollectReport()
816+
.. autoclass:: pytest.CollectReport()
817817
:members:
818818
:show-inheritance:
819819
:inherited-members:
@@ -951,7 +951,7 @@ Session
951951
TestReport
952952
~~~~~~~~~~
953953

954-
.. autoclass:: _pytest.reports.TestReport()
954+
.. autoclass:: pytest.TestReport()
955955
:members:
956956
:show-inheritance:
957957
:inherited-members:

src/_pytest/hookspec.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ def pytest_deselected(items: Sequence["Item"]) -> None:
323323

324324
@hookspec(firstresult=True)
325325
def pytest_make_collect_report(collector: "Collector") -> "Optional[CollectReport]":
326-
"""Perform ``collector.collect()`` and return a CollectReport.
326+
"""Perform :func:`collector.collect() <pytest.Collector.collect>` and return
327+
a :class:`~pytest.CollectReport`.
327328
328329
Stops at first non-None result, see :ref:`firstresult`.
329330
"""
@@ -522,19 +523,19 @@ def pytest_runtest_teardown(item: "Item", nextitem: Optional["Item"]) -> None:
522523
def pytest_runtest_makereport(
523524
item: "Item", call: "CallInfo[None]"
524525
) -> Optional["TestReport"]:
525-
"""Called to create a :py:class:`_pytest.reports.TestReport` for each of
526+
"""Called to create a :class:`~pytest.TestReport` for each of
526527
the setup, call and teardown runtest phases of a test item.
527528
528529
See :func:`pytest_runtest_protocol` for a description of the runtest protocol.
529530
530-
:param CallInfo[None] call: The ``CallInfo`` for the phase.
531+
:param call: The :class:`~pytest.CallInfo` for the phase.
531532
532533
Stops at first non-None result, see :ref:`firstresult`.
533534
"""
534535

535536

536537
def pytest_runtest_logreport(report: "TestReport") -> None:
537-
"""Process the :py:class:`_pytest.reports.TestReport` produced for each
538+
"""Process the :class:`~pytest.TestReport` produced for each
538539
of the setup, call and teardown runtest phases of an item.
539540
540541
See :func:`pytest_runtest_protocol` for a description of the runtest protocol.
@@ -555,7 +556,8 @@ def pytest_report_from_serializable(
555556
config: "Config",
556557
data: Dict[str, Any],
557558
) -> Optional[Union["CollectReport", "TestReport"]]:
558-
"""Restore a report object previously serialized with pytest_report_to_serializable()."""
559+
"""Restore a report object previously serialized with
560+
:func:`pytest_report_to_serializable`."""
559561

560562

561563
# -------------------------------------------------------------------------
@@ -753,7 +755,7 @@ def pytest_report_teststatus(
753755
for example ``"rerun", "R", ("RERUN", {"yellow": True})``.
754756
755757
:param report: The report object whose status is to be returned.
756-
:param pytest.Config config: The pytest config object.
758+
:param config: The pytest config object.
757759
758760
Stops at first non-None result, see :ref:`firstresult`.
759761
"""
@@ -894,10 +896,10 @@ def pytest_exception_interact(
894896
interactively handled.
895897
896898
May be called during collection (see :py:func:`pytest_make_collect_report`),
897-
in which case ``report`` is a :py:class:`_pytest.reports.CollectReport`.
899+
in which case ``report`` is a :class:`CollectReport`.
898900
899901
May be called during runtest of an item (see :py:func:`pytest_runtest_protocol`),
900-
in which case ``report`` is a :py:class:`_pytest.reports.TestReport`.
902+
in which case ``report`` is a :class:`TestReport`.
901903
902904
This hook is not called if the exception that was raised is an internal
903905
exception like ``skip.Exception``.

src/_pytest/reports.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,22 @@ def capstderr(self) -> str:
143143

144144
@property
145145
def passed(self) -> bool:
146+
"""Whether the outcome is passed."""
146147
return self.outcome == "passed"
147148

148149
@property
149150
def failed(self) -> bool:
151+
"""Whether the outcome is failed."""
150152
return self.outcome == "failed"
151153

152154
@property
153155
def skipped(self) -> bool:
156+
"""Whether the outcome is skipped."""
154157
return self.outcome == "skipped"
155158

156159
@property
157160
def fspath(self) -> str:
161+
"""The path portion of the reported node, as a string."""
158162
return self.nodeid.split("::")[0]
159163

160164
@property
@@ -237,7 +241,10 @@ def _report_unserialization_failure(
237241
@final
238242
class TestReport(BaseReport):
239243
"""Basic test report object (also used for setup and teardown calls if
240-
they fail)."""
244+
they fail).
245+
246+
Reports can contain arbitrary extra attributes.
247+
"""
241248

242249
__test__ = False
243250

@@ -354,7 +361,10 @@ def from_item_and_call(cls, item: Item, call: "CallInfo[None]") -> "TestReport":
354361

355362
@final
356363
class CollectReport(BaseReport):
357-
"""Collection report object."""
364+
"""Collection report object.
365+
366+
Reports can contain arbitrary extra attributes.
367+
"""
358368

359369
when = "collect"
360370

src/pytest/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
from _pytest.recwarn import deprecated_call
5858
from _pytest.recwarn import WarningsRecorder
5959
from _pytest.recwarn import warns
60+
from _pytest.reports import CollectReport
61+
from _pytest.reports import TestReport
6062
from _pytest.runner import CallInfo
6163
from _pytest.stash import Stash
6264
from _pytest.stash import StashKey
@@ -86,6 +88,7 @@
8688
"cmdline",
8789
"collect",
8890
"Collector",
91+
"CollectReport",
8992
"Config",
9093
"console_main",
9194
"deprecated_call",
@@ -143,6 +146,7 @@
143146
"StashKey",
144147
"version_tuple",
145148
"TempPathFactory",
149+
"TestReport",
146150
"UsageError",
147151
"WarningsRecorder",
148152
"warns",

0 commit comments

Comments
 (0)