Skip to content

Commit c65bf43

Browse files
committed
Allow custom fixture names for fixtures
When defining a fixture in the same module as where it is used, the function argument shadows the fixture name, which a) annoys pylint and b) can lead to bugs where you forget pull a fixture into a test method. This allows one to define fixtures with a custom name override, bypassing that problem.
1 parent c2b9196 commit c65bf43

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

_pytest/python.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,13 @@ def safe_getattr(object, name, default):
114114

115115
class FixtureFunctionMarker:
116116
def __init__(self, scope, params,
117-
autouse=False, yieldctx=False, ids=None):
117+
autouse=False, yieldctx=False, ids=None, name=None):
118118
self.scope = scope
119119
self.params = params
120120
self.autouse = autouse
121121
self.yieldctx = yieldctx
122122
self.ids = ids
123+
self.name = name
123124

124125
def __call__(self, function):
125126
if isclass(function):
@@ -129,7 +130,7 @@ def __call__(self, function):
129130
return function
130131

131132

132-
def fixture(scope="function", params=None, autouse=False, ids=None):
133+
def fixture(scope="function", params=None, autouse=False, ids=None, name=None):
133134
""" (return a) decorator to mark a fixture factory function.
134135
135136
This decorator can be used (with or or without parameters) to define
@@ -159,10 +160,10 @@ def fixture(scope="function", params=None, autouse=False, ids=None):
159160
if callable(scope) and params is None and autouse == False:
160161
# direct decoration
161162
return FixtureFunctionMarker(
162-
"function", params, autouse)(scope)
163+
"function", params, autouse, name=name)(scope)
163164
if params is not None and not isinstance(params, (list, tuple)):
164165
params = list(params)
165-
return FixtureFunctionMarker(scope, params, autouse, ids=ids)
166+
return FixtureFunctionMarker(scope, params, autouse, ids=ids, name=name)
166167

167168
def yield_fixture(scope="function", params=None, autouse=False, ids=None):
168169
""" (return a) decorator to mark a yield-fixture factory function
@@ -1989,6 +1990,8 @@ def parsefactories(self, node_or_obj, nodeid=NOTSET, unittest=False):
19891990
# fixture attribute
19901991
continue
19911992
else:
1993+
if marker.name:
1994+
name = marker.name
19921995
assert not name.startswith(self._argprefix)
19931996
fixturedef = FixtureDef(self, nodeid, name, obj,
19941997
marker.scope, marker.params,

testing/python/fixture.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,3 +2691,14 @@ def test_1(arg1):
26912691
*def arg1*
26922692
""")
26932693

2694+
def test_custom_name(self, testdir):
2695+
testdir.makepyfile("""
2696+
import pytest
2697+
@pytest.fixture(name='meow')
2698+
def arg1():
2699+
return 'mew'
2700+
def test_1(meow):
2701+
print(meow)
2702+
""")
2703+
result = testdir.runpytest("-s")
2704+
result.stdout.fnmatch_lines("*mew*")

0 commit comments

Comments
 (0)