Skip to content
This repository was archived by the owner on Apr 1, 2021. It is now read-only.

Commit e5169a0

Browse files
pytest-dev#2574: --fixtures, --fixtures-per-test keep indentation of docstring
1 parent 3578f4e commit e5169a0

File tree

2 files changed

+81
-10
lines changed

2 files changed

+81
-10
lines changed

_pytest/python.py

+20-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import collections
99
import math
10+
from textwrap import dedent
1011
from itertools import count
1112

1213
import py
@@ -1003,14 +1004,12 @@ def write_fixture(fixture_def):
10031004
funcargspec = argname
10041005
tw.line(funcargspec, green=True)
10051006

1006-
INDENT = ' {0}'
10071007
fixture_doc = fixture_def.func.__doc__
10081008

10091009
if fixture_doc:
1010-
for line in fixture_doc.strip().split('\n'):
1011-
tw.line(INDENT.format(line.strip()))
1010+
write_docstring(tw, fixture_doc)
10121011
else:
1013-
tw.line(INDENT.format('no docstring available'), red=True)
1012+
tw.line(' no docstring available', red=True)
10141013

10151014
def write_item(item):
10161015
name2fixturedefs = item._fixtureinfo.name2fixturedefs
@@ -1084,13 +1083,28 @@ def _showfixtures_main(config, session):
10841083
loc = getlocation(fixturedef.func, curdir)
10851084
doc = fixturedef.func.__doc__ or ""
10861085
if doc:
1087-
for line in doc.strip().split("\n"):
1088-
tw.line(" " + line.strip())
1086+
write_docstring(tw, doc)
10891087
else:
10901088
tw.line(" %s: no docstring available" %(loc,),
10911089
red=True)
10921090

10931091

1092+
def write_docstring(tw, doc):
1093+
INDENT = " "
1094+
doc = doc.rstrip()
1095+
if "\n" in doc:
1096+
firstline, rest = doc.split("\n", 1)
1097+
else:
1098+
firstline, rest = doc, ""
1099+
1100+
if firstline.strip():
1101+
tw.line(INDENT + firstline.strip())
1102+
1103+
if rest:
1104+
for line in dedent(rest).split("\n"):
1105+
tw.write(INDENT + line + "\n")
1106+
1107+
10941108
# builtin pytest.raises helper
10951109

10961110
def raises(expected_exception, *args, **kwargs):

testing/python/fixture.py

+61-4
Original file line numberDiff line numberDiff line change
@@ -2713,7 +2713,7 @@ def test_hello():
27132713
""")
27142714

27152715
def test_show_fixtures_trimmed_doc(self, testdir):
2716-
p = testdir.makepyfile('''
2716+
p = testdir.makepyfile(dedent('''
27172717
import pytest
27182718
@pytest.fixture
27192719
def arg1():
@@ -2729,9 +2729,9 @@ def arg2():
27292729
line2
27302730
27312731
"""
2732-
''')
2732+
'''))
27332733
result = testdir.runpytest("--fixtures", p)
2734-
result.stdout.fnmatch_lines("""
2734+
result.stdout.fnmatch_lines(dedent("""
27352735
* fixtures defined from test_show_fixtures_trimmed_doc *
27362736
arg2
27372737
line1
@@ -2740,7 +2740,64 @@ def arg2():
27402740
line1
27412741
line2
27422742
2743-
""")
2743+
"""))
2744+
2745+
def test_show_fixtures_indented_doc(self, testdir):
2746+
p = testdir.makepyfile(dedent('''
2747+
import pytest
2748+
@pytest.fixture
2749+
def fixture1():
2750+
"""
2751+
line1
2752+
indented line
2753+
"""
2754+
'''))
2755+
result = testdir.runpytest("--fixtures", p)
2756+
result.stdout.fnmatch_lines(dedent("""
2757+
* fixtures defined from test_show_fixtures_indented_doc *
2758+
fixture1
2759+
line1
2760+
indented line
2761+
"""))
2762+
2763+
def test_show_fixtures_indented_doc_first_line_unindented(self, testdir):
2764+
p = testdir.makepyfile(dedent('''
2765+
import pytest
2766+
@pytest.fixture
2767+
def fixture1():
2768+
"""line1
2769+
line2
2770+
indented line
2771+
"""
2772+
'''))
2773+
result = testdir.runpytest("--fixtures", p)
2774+
result.stdout.fnmatch_lines(dedent("""
2775+
* fixtures defined from test_show_fixtures_indented_doc_first_line_unindented *
2776+
fixture1
2777+
line1
2778+
line2
2779+
indented line
2780+
"""))
2781+
2782+
def test_show_fixtures_indented_in_class(self, testdir):
2783+
p = testdir.makepyfile(dedent('''
2784+
import pytest
2785+
class TestClass:
2786+
@pytest.fixture
2787+
def fixture1():
2788+
"""line1
2789+
line2
2790+
indented line
2791+
"""
2792+
'''))
2793+
result = testdir.runpytest("--fixtures", p)
2794+
result.stdout.fnmatch_lines(dedent("""
2795+
* fixtures defined from test_show_fixtures_indented_in_class *
2796+
fixture1
2797+
line1
2798+
line2
2799+
indented line
2800+
"""))
27442801

27452802

27462803
def test_show_fixtures_different_files(self, testdir):

0 commit comments

Comments
 (0)