@@ -114,12 +114,13 @@ def safe_getattr(object, name, default):
114
114
115
115
class FixtureFunctionMarker :
116
116
def __init__ (self , scope , params ,
117
- autouse = False , yieldctx = False , ids = None ):
117
+ autouse = False , yieldctx = False , ids = None , name = None ):
118
118
self .scope = scope
119
119
self .params = params
120
120
self .autouse = autouse
121
121
self .yieldctx = yieldctx
122
122
self .ids = ids
123
+ self .name = name
123
124
124
125
def __call__ (self , function ):
125
126
if isclass (function ):
@@ -129,7 +130,7 @@ def __call__(self, function):
129
130
return function
130
131
131
132
132
- def fixture (scope = "function" , params = None , autouse = False , ids = None ):
133
+ def fixture (scope = "function" , params = None , autouse = False , ids = None , name = None ):
133
134
""" (return a) decorator to mark a fixture factory function.
134
135
135
136
This decorator can be used (with or or without parameters) to define
@@ -155,14 +156,21 @@ def fixture(scope="function", params=None, autouse=False, ids=None):
155
156
so that they are part of the test id. If no ids are provided
156
157
they will be generated automatically from the params.
157
158
159
+ :arg name: the name of the fixture. This defaults to the name of the
160
+ decorated function. If a fixture is used in the same module in
161
+ which it is defined, the function name of the fixture will be
162
+ shadowed by the function arg that requests the fixture; one way
163
+ to resolve this is to name the decorated function
164
+ ``fixture_<fixturename>`` and then use
165
+ ``@pytest.fixture(name='<fixturename>')``.
158
166
"""
159
167
if callable (scope ) and params is None and autouse == False :
160
168
# direct decoration
161
169
return FixtureFunctionMarker (
162
- "function" , params , autouse )(scope )
170
+ "function" , params , autouse , name = name )(scope )
163
171
if params is not None and not isinstance (params , (list , tuple )):
164
172
params = list (params )
165
- return FixtureFunctionMarker (scope , params , autouse , ids = ids )
173
+ return FixtureFunctionMarker (scope , params , autouse , ids = ids , name = name )
166
174
167
175
def yield_fixture (scope = "function" , params = None , autouse = False , ids = None ):
168
176
""" (return a) decorator to mark a yield-fixture factory function
@@ -1989,6 +1997,8 @@ def parsefactories(self, node_or_obj, nodeid=NOTSET, unittest=False):
1989
1997
# fixture attribute
1990
1998
continue
1991
1999
else :
2000
+ if marker .name :
2001
+ name = marker .name
1992
2002
assert not name .startswith (self ._argprefix )
1993
2003
fixturedef = FixtureDef (self , nodeid , name , obj ,
1994
2004
marker .scope , marker .params ,
0 commit comments