Skip to content

Commit 3cd2e37

Browse files
authored
Merge pull request #1783 from nicoddemus/inv-scoped-fixture-msg
Strip invocation-scope suffix when displaying fixture lookup error
2 parents 226f279 + 553dc26 commit 3cd2e37

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

_pytest/fixtures.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,16 @@ def scopemismatch(currentscope, newscope):
615615
return scopes.index(newscope) > scopes.index(currentscope)
616616

617617

618+
def strip_invocation_scope_suffix(name):
619+
"""Remove the invocation-scope suffix from the given name.
620+
621+
Invocation scope fixtures have their scope in the name of the fixture.
622+
For example, "monkeypatch:session". This function strips the suffix
623+
returning the user-frienldy name of the fixture.
624+
"""
625+
return name.split(':')[0]
626+
627+
618628
class FixtureLookupError(LookupError):
619629
""" could not return a requested Fixture (missing or invalid). """
620630
def __init__(self, argname, request, msg=None):
@@ -654,7 +664,8 @@ def formatrepr(self):
654664
parentid = self.request._pyfuncitem.parent.nodeid
655665
for name, fixturedefs in fm._arg2fixturedefs.items():
656666
faclist = list(fm._matchfactories(fixturedefs, parentid))
657-
if faclist:
667+
name = strip_invocation_scope_suffix(name)
668+
if faclist and name not in available:
658669
available.append(name)
659670
msg = "fixture %r not found" % (self.argname,)
660671
msg += "\n available fixtures: %s" %(", ".join(available),)

_pytest/python.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,7 @@ def showfixtures(config):
10061006

10071007
def _showfixtures_main(config, session):
10081008
import _pytest.config
1009+
from _pytest.fixtures import strip_invocation_scope_suffix
10091010
session.perform_collect()
10101011
curdir = py.path.local()
10111012
tw = _pytest.config.create_terminal_writer(config)
@@ -1022,11 +1023,9 @@ def _showfixtures_main(config, session):
10221023
continue
10231024
for fixturedef in fixturedefs:
10241025
loc = getlocation(fixturedef.func, curdir)
1025-
fixture_argname = fixturedef.argname
10261026
# invocation-scoped fixtures have argname in the form
10271027
# "<name>:<scope>" (for example: "monkeypatch:session").
1028-
if ':' in fixture_argname:
1029-
fixture_argname = fixture_argname.split(':')[0]
1028+
fixture_argname = strip_invocation_scope_suffix(fixturedef.argname)
10301029
if (fixture_argname, loc) in seen:
10311030
continue
10321031
seen.add((fixture_argname, loc))

testing/python/fixture.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ def test_lookup_error(unknown):
417417
"*1 error*",
418418
])
419419
assert "INTERNAL" not in result.stdout.str()
420+
assert 'monkeypatch:session' not in result.stdout.str()
420421

421422
def test_fixture_excinfo_leak(self, testdir):
422423
# on python2 sys.excinfo would leak into fixture executions

0 commit comments

Comments
 (0)