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

View File

@ -43,7 +43,7 @@ install: html
rsync -avz _build/html/ pytest.org:/www/pytest.org/dev rsync -avz _build/html/ pytest.org:/www/pytest.org/dev
installpdf: latexpdf 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 installall: clean install installpdf
@echo "done" @echo "done"

View File

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

View File

@ -1515,7 +1515,8 @@ class TestReportInfo:
pass pass
""" """
def test_show_funcarg(testdir): class TestShowFixtures:
def test_show_fixtures(self, testdir):
result = testdir.runpytest("--fixtures") result = testdir.runpytest("--fixtures")
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
"*tmpdir*", "*tmpdir*",
@ -1523,6 +1524,42 @@ def test_show_funcarg(testdir):
] ]
) )
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: class TestRaises:
def test_raises(self): def test_raises(self):
source = "int('qwe')" source = "int('qwe')"