Skip to content

Commit

Permalink
Export CollectReport and TestReport
Browse files Browse the repository at this point in the history
Refs #7469.
  • Loading branch information
bluetech committed Nov 2, 2021
1 parent a53abe9 commit b0aa870
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
2 changes: 2 additions & 0 deletions changelog/7469.feature.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ The newly-exported types are:
- ``pytest.RecordedHookCall`` for the :class:`RecordedHookCall <pytest.HookRecorder>` type returned from :class:`~pytest.HookRecorder`.
- ``pytest.RunResult`` for the :class:`RunResult <pytest.RunResult>` type returned from :class:`~pytest.Pytester`.
- ``pytest.LineMatcher`` for the :class:`LineMatcher <pytest.RunResult>` type used in :class:`~pytest.RunResult` and others.
- ``pytest.TestReport`` for the :class:`TestReport <pytest.TestReport>` type used in various hooks.
- ``pytest.CollectReport`` for the :class:`CollectReport <pytest.CollectReport>` type used in various hooks.

Constructing most of them directly is not supported; they are only meant for use in type annotations.
Doing so will emit a deprecation warning, and may become a hard-error in pytest 8.0.
Expand Down
4 changes: 2 additions & 2 deletions doc/en/reference/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ Collector
CollectReport
~~~~~~~~~~~~~

.. autoclass:: _pytest.reports.CollectReport()
.. autoclass:: pytest.CollectReport()
:members:
:show-inheritance:
:inherited-members:
Expand Down Expand Up @@ -951,7 +951,7 @@ Session
TestReport
~~~~~~~~~~

.. autoclass:: _pytest.reports.TestReport()
.. autoclass:: pytest.TestReport()
:members:
:show-inheritance:
:inherited-members:
Expand Down
18 changes: 10 additions & 8 deletions src/_pytest/hookspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ def pytest_deselected(items: Sequence["Item"]) -> None:

@hookspec(firstresult=True)
def pytest_make_collect_report(collector: "Collector") -> "Optional[CollectReport]":
"""Perform ``collector.collect()`` and return a CollectReport.
"""Perform :func:`collector.collect() <pytest.Collector.collect>` and return
a :class:`~pytest.CollectReport`.
Stops at first non-None result, see :ref:`firstresult`.
"""
Expand Down Expand Up @@ -522,19 +523,19 @@ def pytest_runtest_teardown(item: "Item", nextitem: Optional["Item"]) -> None:
def pytest_runtest_makereport(
item: "Item", call: "CallInfo[None]"
) -> Optional["TestReport"]:
"""Called to create a :py:class:`_pytest.reports.TestReport` for each of
"""Called to create a :class:`~pytest.TestReport` for each of
the setup, call and teardown runtest phases of a test item.
See :func:`pytest_runtest_protocol` for a description of the runtest protocol.
:param CallInfo[None] call: The ``CallInfo`` for the phase.
:param call: The :class:`~pytest.CallInfo` for the phase.
Stops at first non-None result, see :ref:`firstresult`.
"""


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


# -------------------------------------------------------------------------
Expand Down Expand Up @@ -753,7 +755,7 @@ def pytest_report_teststatus(
for example ``"rerun", "R", ("RERUN", {"yellow": True})``.
:param report: The report object whose status is to be returned.
:param pytest.Config config: The pytest config object.
:param config: The pytest config object.
Stops at first non-None result, see :ref:`firstresult`.
"""
Expand Down Expand Up @@ -894,10 +896,10 @@ def pytest_exception_interact(
interactively handled.
May be called during collection (see :py:func:`pytest_make_collect_report`),
in which case ``report`` is a :py:class:`_pytest.reports.CollectReport`.
in which case ``report`` is a :class:`CollectReport`.
May be called during runtest of an item (see :py:func:`pytest_runtest_protocol`),
in which case ``report`` is a :py:class:`_pytest.reports.TestReport`.
in which case ``report`` is a :class:`TestReport`.
This hook is not called if the exception that was raised is an internal
exception like ``skip.Exception``.
Expand Down
14 changes: 12 additions & 2 deletions src/_pytest/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,22 @@ def capstderr(self) -> str:

@property
def passed(self) -> bool:
"""Whether the outcome is passed."""
return self.outcome == "passed"

@property
def failed(self) -> bool:
"""Whether the outcome is failed."""
return self.outcome == "failed"

@property
def skipped(self) -> bool:
"""Whether the outcome is skipped."""
return self.outcome == "skipped"

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

@property
Expand Down Expand Up @@ -237,7 +241,10 @@ def _report_unserialization_failure(
@final
class TestReport(BaseReport):
"""Basic test report object (also used for setup and teardown calls if
they fail)."""
they fail).
Reports can contain arbitrary extra attributes.
"""

__test__ = False

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

@final
class CollectReport(BaseReport):
"""Collection report object."""
"""Collection report object.
Reports can contain arbitrary extra attributes.
"""

when = "collect"

Expand Down
4 changes: 4 additions & 0 deletions src/pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
from _pytest.recwarn import deprecated_call
from _pytest.recwarn import WarningsRecorder
from _pytest.recwarn import warns
from _pytest.reports import CollectReport
from _pytest.reports import TestReport
from _pytest.runner import CallInfo
from _pytest.stash import Stash
from _pytest.stash import StashKey
Expand Down Expand Up @@ -86,6 +88,7 @@
"cmdline",
"collect",
"Collector",
"CollectReport",
"Config",
"console_main",
"deprecated_call",
Expand Down Expand Up @@ -143,6 +146,7 @@
"StashKey",
"version_tuple",
"TempPathFactory",
"TestReport",
"UsageError",
"WarningsRecorder",
"warns",
Expand Down

0 comments on commit b0aa870

Please sign in to comment.