diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 52c04a49c..a1e4b1562 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -5,6 +5,7 @@ This is a good source for looking at the various reporting hooks. import argparse import collections import datetime +import inspect import platform import sys import time @@ -707,9 +708,14 @@ class TerminalReporter: indent = (len(stack) - 1) * " " self._tw.line("{}{}".format(indent, col)) if self.config.option.verbose >= 1: - if hasattr(col, "_obj") and col._obj.__doc__: - for line in col._obj.__doc__.strip().splitlines(): - self._tw.line("{}{}".format(indent + " ", line.strip())) + try: + obj = col.obj # type: ignore + except AttributeError: + continue + doc = inspect.getdoc(obj) + if doc: + for line in doc.splitlines(): + self._tw.line("{}{}".format(indent + " ", line)) @pytest.hookimpl(hookwrapper=True) def pytest_sessionfinish(self, session: Session, exitstatus: ExitCode): diff --git a/testing/test_terminal.py b/testing/test_terminal.py index d2857ca77..0f5b4cb68 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -353,17 +353,33 @@ class TestCollectonly: result = testdir.runpytest("--collect-only", "-rs") result.stdout.fnmatch_lines(["*ERROR collecting*"]) - def test_collectonly_display_test_description(self, testdir): + def test_collectonly_displays_test_description( + self, testdir: Testdir, dummy_yaml_custom_test + ) -> None: + """Used dummy_yaml_custom_test for an Item without ``obj``.""" testdir.makepyfile( """ def test_with_description(): - \""" This test has a description. - \""" - assert True - """ + ''' This test has a description. + + more1. + more2.''' + """ ) result = testdir.runpytest("--collect-only", "--verbose") - result.stdout.fnmatch_lines([" This test has a description."]) + result.stdout.fnmatch_lines( + [ + "", + " ", + "", + " ", + " This test has a description.", + " ", + " more1.", + " more2.", + ], + consecutive=True, + ) def test_collectonly_failed_module(self, testdir): testdir.makepyfile("""raise ValueError(0)""")