From 553dc2600f1609551d900b129ef2704137846183 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 1 Aug 2016 19:51:35 -0300 Subject: [PATCH] Strip invocation-scope suffix when displaying fixture lookup error --- _pytest/fixtures.py | 13 ++++++++++++- _pytest/python.py | 5 ++--- testing/python/fixture.py | 1 + 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/_pytest/fixtures.py b/_pytest/fixtures.py index d4c70f590..427d2e5d9 100644 --- a/_pytest/fixtures.py +++ b/_pytest/fixtures.py @@ -615,6 +615,16 @@ def scopemismatch(currentscope, newscope): 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): """ could not return a requested Fixture (missing or invalid). """ def __init__(self, argname, request, msg=None): @@ -654,7 +664,8 @@ class FixtureLookupError(LookupError): parentid = self.request._pyfuncitem.parent.nodeid for name, fixturedefs in fm._arg2fixturedefs.items(): faclist = list(fm._matchfactories(fixturedefs, parentid)) - if faclist: + name = strip_invocation_scope_suffix(name) + if faclist and name not in available: available.append(name) msg = "fixture %r not found" % (self.argname,) msg += "\n available fixtures: %s" %(", ".join(available),) diff --git a/_pytest/python.py b/_pytest/python.py index ca076aa5f..d3789109e 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -1006,6 +1006,7 @@ def showfixtures(config): def _showfixtures_main(config, session): import _pytest.config + from _pytest.fixtures import strip_invocation_scope_suffix session.perform_collect() curdir = py.path.local() tw = _pytest.config.create_terminal_writer(config) @@ -1022,11 +1023,9 @@ def _showfixtures_main(config, session): continue for fixturedef in fixturedefs: loc = getlocation(fixturedef.func, curdir) - fixture_argname = fixturedef.argname # invocation-scoped fixtures have argname in the form # ":" (for example: "monkeypatch:session"). - if ':' in fixture_argname: - fixture_argname = fixture_argname.split(':')[0] + fixture_argname = strip_invocation_scope_suffix(fixturedef.argname) if (fixture_argname, loc) in seen: continue seen.add((fixture_argname, loc)) diff --git a/testing/python/fixture.py b/testing/python/fixture.py index 22344efb9..0b6829031 100644 --- a/testing/python/fixture.py +++ b/testing/python/fixture.py @@ -417,6 +417,7 @@ class TestFillFixtures: "*1 error*", ]) assert "INTERNAL" not in result.stdout.str() + assert 'monkeypatch:session' not in result.stdout.str() def test_fixture_excinfo_leak(self, testdir): # on python2 sys.excinfo would leak into fixture executions