fix output of --fixtures for @pytest.fixture defined functions.

This commit is contained in:
holger krekel 2012-10-09 16:49:04 +02:00
parent fb3af07ef4
commit 0594265adc
5 changed files with 83 additions and 38 deletions

View File

@ -1,2 +1,2 @@
#
__version__ = '2.3.0.dev20'
__version__ = '2.3.0.dev21'

View File

@ -742,37 +742,45 @@ def showfixtures(config):
def _showfixtures_main(config, session):
session.perform_collect()
if session.items:
plugins = session.items[0].getplugins()
else:
plugins = session.getplugins()
curdir = py.path.local()
if session.items:
nodeid = session.items[0].nodeid
else:
part = session._initialparts[0]
nodeid = "::".join(map(str, [curdir.bestrelpath(part[0])] + part[1:]))
tw = py.io.TerminalWriter()
verbose = config.getvalue("verbose")
argprefix = session._fixturemanager._argprefix
for plugin in plugins:
available = []
for name, factory in vars(plugin).items():
if name.startswith(argprefix):
name = name[len(argprefix):]
if name not in available:
available.append([name, factory])
if available:
pluginname = plugin.__name__
for name, factory in available:
loc = getlocation(factory, curdir)
if verbose > 0:
funcargspec = "%s -- %s" %(name, loc,)
else:
funcargspec = name
tw.line(funcargspec, green=True)
doc = factory.__doc__ or ""
if doc:
for line in doc.split("\n"):
tw.line(" " + line.strip())
else:
tw.line(" %s: no docstring available" %(loc,),
red=True)
fm = session._fixturemanager
available = []
for argname in fm.arg2fixturedeflist:
fixturedeflist = fm.getfixturedeflist(argname, nodeid)
assert fixturedeflist is not None
if not fixturedeflist:
continue
fixturedef = fixturedeflist[-1]
loc = getlocation(fixturedef.func, curdir)
available.append((len(fixturedef.baseid),
curdir.bestrelpath(loc),
fixturedef.argname, fixturedef))
available.sort()
for baseid, bestrel, argname, fixturedef in available:
if verbose > 0:
funcargspec = "%s -- %s" %(name, loc,)
else:
funcargspec = argname # "%s %s" %(baseid, argname)
tw.line(funcargspec, green=True)
loc = getlocation(fixturedef.func, curdir)
doc = fixturedef.func.__doc__ or ""
if doc:
for line in doc.split("\n"):
tw.line(" " + line.strip())
else:
tw.line(" %s: no docstring available" %(loc,),
red=True)
def getlocation(function, curdir):
import inspect

View File

@ -43,7 +43,7 @@ install: html
rsync -avz _build/html/ pytest.org:/www/pytest.org/dev
installpdf: latexpdf
@scp $(BUILDDIR)/latex/pytest.pdf pytest.org:/www/pytest.org/latest
@scp $(BUILDDIR)/latex/pytest.pdf pytest.org:/www/pytest.org/dev
installall: clean install installpdf
@echo "done"

View File

@ -24,7 +24,7 @@ def main():
name='pytest',
description='py.test: simple powerful testing with Python',
long_description = long_description,
version='2.3.0.dev20',
version='2.3.0.dev21',
url='http://pytest.org',
license='MIT license',
platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],

View File

@ -1515,13 +1515,50 @@ class TestReportInfo:
pass
"""
def test_show_funcarg(testdir):
result = testdir.runpytest("--fixtures")
result.stdout.fnmatch_lines([
"*tmpdir*",
"*temporary directory*",
]
)
class TestShowFixtures:
def test_show_fixtures(self, testdir):
result = testdir.runpytest("--fixtures")
result.stdout.fnmatch_lines([
"*tmpdir*",
"*temporary directory*",
]
)
def test_show_fixtures_testmodule(self, testdir):
p = testdir.makepyfile('''
import pytest
@pytest.fixture
def arg1():
""" hello world """
''')
result = testdir.runpytest("--fixtures", p)
result.stdout.fnmatch_lines("""
*tmpdir*
*arg1*
*hello world*
""")
@pytest.mark.parametrize("testmod", [True, False])
def test_show_fixtures_conftest(self, testdir, testmod):
testdir.makeconftest('''
import pytest
@pytest.fixture
def arg1():
""" hello world """
''')
if testmod:
testdir.makepyfile("""
def test_hello():
pass
""")
result = testdir.runpytest("--fixtures")
result.stdout.fnmatch_lines("""
*tmpdir*
*arg1*
*hello world*
""")
class TestRaises:
def test_raises(self):