Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2574 Options --fixtures and --fixtures-per-test keep indentation of docstrings #2575

Merged
merged 3 commits into from
Jul 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Marcin Bachry
Mark Abramowitz
Markus Unterwaditzer
Martijn Faassen
Martin Altmayer
Martin K. Scherer
Martin Prusse
Mathieu Clabaut
Expand Down
26 changes: 20 additions & 6 deletions _pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import collections
import math
from textwrap import dedent
from itertools import count

import py
Expand Down Expand Up @@ -1003,14 +1004,12 @@ def write_fixture(fixture_def):
funcargspec = argname
tw.line(funcargspec, green=True)

INDENT = ' {0}'
fixture_doc = fixture_def.func.__doc__

if fixture_doc:
for line in fixture_doc.strip().split('\n'):
tw.line(INDENT.format(line.strip()))
write_docstring(tw, fixture_doc)
else:
tw.line(INDENT.format('no docstring available'), red=True)
tw.line(' no docstring available', red=True)

def write_item(item):
name2fixturedefs = item._fixtureinfo.name2fixturedefs
Expand Down Expand Up @@ -1084,13 +1083,28 @@ def _showfixtures_main(config, session):
loc = getlocation(fixturedef.func, curdir)
doc = fixturedef.func.__doc__ or ""
if doc:
for line in doc.strip().split("\n"):
tw.line(" " + line.strip())
write_docstring(tw, doc)
else:
tw.line(" %s: no docstring available" %(loc,),
red=True)


def write_docstring(tw, doc):
INDENT = " "
doc = doc.rstrip()
if "\n" in doc:
firstline, rest = doc.split("\n", 1)
else:
firstline, rest = doc, ""

if firstline.strip():
tw.line(INDENT + firstline.strip())

if rest:
for line in dedent(rest).split("\n"):
tw.write(INDENT + line + "\n")


# builtin pytest.raises helper

def raises(expected_exception, *args, **kwargs):
Expand Down
1 change: 1 addition & 0 deletions changelog/2574.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The options --fixtures and --fixtures-per-test will now keep indentation within docstrings.
65 changes: 61 additions & 4 deletions testing/python/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -2713,7 +2713,7 @@ def test_hello():
""")

def test_show_fixtures_trimmed_doc(self, testdir):
p = testdir.makepyfile('''
p = testdir.makepyfile(dedent('''
import pytest
@pytest.fixture
def arg1():
Expand All @@ -2729,9 +2729,9 @@ def arg2():
line2

"""
''')
'''))
result = testdir.runpytest("--fixtures", p)
result.stdout.fnmatch_lines("""
result.stdout.fnmatch_lines(dedent("""
* fixtures defined from test_show_fixtures_trimmed_doc *
arg2
line1
Expand All @@ -2740,7 +2740,64 @@ def arg2():
line1
line2

""")
"""))

def test_show_fixtures_indented_doc(self, testdir):
p = testdir.makepyfile(dedent('''
import pytest
@pytest.fixture
def fixture1():
"""
line1
indented line
"""
'''))
result = testdir.runpytest("--fixtures", p)
result.stdout.fnmatch_lines(dedent("""
* fixtures defined from test_show_fixtures_indented_doc *
fixture1
line1
indented line
"""))

def test_show_fixtures_indented_doc_first_line_unindented(self, testdir):
p = testdir.makepyfile(dedent('''
import pytest
@pytest.fixture
def fixture1():
"""line1
line2
indented line
"""
'''))
result = testdir.runpytest("--fixtures", p)
result.stdout.fnmatch_lines(dedent("""
* fixtures defined from test_show_fixtures_indented_doc_first_line_unindented *
fixture1
line1
line2
indented line
"""))

def test_show_fixtures_indented_in_class(self, testdir):
p = testdir.makepyfile(dedent('''
import pytest
class TestClass:
@pytest.fixture
def fixture1():
"""line1
line2
indented line
"""
'''))
result = testdir.runpytest("--fixtures", p)
result.stdout.fnmatch_lines(dedent("""
* fixtures defined from test_show_fixtures_indented_in_class *
fixture1
line1
line2
indented line
"""))


def test_show_fixtures_different_files(self, testdir):
Expand Down