diff --git a/changelog/4371.feature.rst b/changelog/4371.feature.rst new file mode 100644 index 000000000..f205fc269 --- /dev/null +++ b/changelog/4371.feature.rst @@ -0,0 +1 @@ +Updated the ``--collect-only`` option to display test descriptions when ran using ``--verbose``. diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 82719b5d4..bea02306b 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -611,6 +611,10 @@ class TerminalReporter(object): continue indent = (len(stack) - 1) * " " self._tw.line("%s%s" % (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("%s%s" % (indent + " ", line.strip())) @pytest.hookimpl(hookwrapper=True) def pytest_sessionfinish(self, exitstatus): diff --git a/testing/test_terminal.py b/testing/test_terminal.py index 9cd79afcf..06345f88d 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -276,6 +276,18 @@ class TestCollectonly(object): result = testdir.runpytest("--collect-only", "-rs") result.stdout.fnmatch_lines(["*ERROR collecting*"]) + def test_collectonly_display_test_description(self, testdir): + testdir.makepyfile( + """ + def test_with_description(): + \""" This test has a description. + \""" + assert True + """ + ) + result = testdir.runpytest("--collect-only", "--verbose") + result.stdout.fnmatch_lines([" This test has a description."]) + def test_collectonly_failed_module(self, testdir): testdir.makepyfile("""raise ValueError(0)""") result = testdir.runpytest("--collect-only")