Strip invocation-scope suffix when displaying fixture lookup error

This commit is contained in:
Bruno Oliveira 2016-08-01 19:51:35 -03:00
parent 226f2795ba
commit 553dc2600f
3 changed files with 15 additions and 4 deletions

View File

@ -615,6 +615,16 @@ def scopemismatch(currentscope, newscope):
return scopes.index(newscope) > scopes.index(currentscope) return scopes.index(newscope) > scopes.index(currentscope)
def strip_invocation_scope_suffix(name):
"""Remove the invocation-scope suffix from the given name.
Invocation scope fixtures have their scope in the name of the fixture.
For example, "monkeypatch:session". This function strips the suffix
returning the user-frienldy name of the fixture.
"""
return name.split(':')[0]
class FixtureLookupError(LookupError): class FixtureLookupError(LookupError):
""" could not return a requested Fixture (missing or invalid). """ """ could not return a requested Fixture (missing or invalid). """
def __init__(self, argname, request, msg=None): def __init__(self, argname, request, msg=None):
@ -654,7 +664,8 @@ class FixtureLookupError(LookupError):
parentid = self.request._pyfuncitem.parent.nodeid parentid = self.request._pyfuncitem.parent.nodeid
for name, fixturedefs in fm._arg2fixturedefs.items(): for name, fixturedefs in fm._arg2fixturedefs.items():
faclist = list(fm._matchfactories(fixturedefs, parentid)) faclist = list(fm._matchfactories(fixturedefs, parentid))
if faclist: name = strip_invocation_scope_suffix(name)
if faclist and name not in available:
available.append(name) available.append(name)
msg = "fixture %r not found" % (self.argname,) msg = "fixture %r not found" % (self.argname,)
msg += "\n available fixtures: %s" %(", ".join(available),) msg += "\n available fixtures: %s" %(", ".join(available),)

View File

@ -1006,6 +1006,7 @@ def showfixtures(config):
def _showfixtures_main(config, session): def _showfixtures_main(config, session):
import _pytest.config import _pytest.config
from _pytest.fixtures import strip_invocation_scope_suffix
session.perform_collect() session.perform_collect()
curdir = py.path.local() curdir = py.path.local()
tw = _pytest.config.create_terminal_writer(config) tw = _pytest.config.create_terminal_writer(config)
@ -1022,11 +1023,9 @@ def _showfixtures_main(config, session):
continue continue
for fixturedef in fixturedefs: for fixturedef in fixturedefs:
loc = getlocation(fixturedef.func, curdir) loc = getlocation(fixturedef.func, curdir)
fixture_argname = fixturedef.argname
# invocation-scoped fixtures have argname in the form # invocation-scoped fixtures have argname in the form
# "<name>:<scope>" (for example: "monkeypatch:session"). # "<name>:<scope>" (for example: "monkeypatch:session").
if ':' in fixture_argname: fixture_argname = strip_invocation_scope_suffix(fixturedef.argname)
fixture_argname = fixture_argname.split(':')[0]
if (fixture_argname, loc) in seen: if (fixture_argname, loc) in seen:
continue continue
seen.add((fixture_argname, loc)) seen.add((fixture_argname, loc))

View File

@ -417,6 +417,7 @@ class TestFillFixtures:
"*1 error*", "*1 error*",
]) ])
assert "INTERNAL" not in result.stdout.str() assert "INTERNAL" not in result.stdout.str()
assert 'monkeypatch:session' not in result.stdout.str()
def test_fixture_excinfo_leak(self, testdir): def test_fixture_excinfo_leak(self, testdir):
# on python2 sys.excinfo would leak into fixture executions # on python2 sys.excinfo would leak into fixture executions