From 0b5d2ff526fdff018459ad971da33ca8114b3d80 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 9 Apr 2020 21:47:12 +0200 Subject: [PATCH] Fix/improve printing of docs for collected items --- src/_pytest/terminal.py | 12 +++++++++--- testing/test_terminal.py | 28 ++++++++++++++++++++++------ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index a99463fe8..1273e0cd9 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 88d564519..72c67cc39 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)""")