Skip to content

Commit 0cacdef

Browse files
Merge pull request #1444 from novas0x2a/fixture-custom-name
Allow custom fixture names for fixtures
2 parents c2b9196 + 9577120 commit 0cacdef

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Matt Williams
6363
Michael Aquilina
6464
Michael Birtwell
6565
Michael Droettboom
66+
Mike Lundy
6667
Nicolas Delaby
6768
Pieter Mulder
6869
Piotr Banaszkiewicz

CHANGELOG.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
namespace in which your doctests run.
88
Thanks `@milliams`_ for the complete PR (`#1428`_).
99

10-
*
10+
* New ``name`` argument to ``pytest.fixture`` mark, which allows a custom name
11+
for a fixture (to solve the funcarg-shadowing-fixture problem).
12+
Thanks `@novas0x2a`_ for the complete PR (`#1444`_).
13+
14+
*
1115

1216
*
1317

@@ -21,8 +25,10 @@
2125
*
2226

2327
.. _@milliams: https://github.com/milliams
28+
.. _@novas0x2a: https://github.com/novas0x2a
2429

2530
.. _#1428: https://github.com/pytest-dev/pytest/pull/1428
31+
.. _#1444: https://github.com/pytest-dev/pytest/pull/1444
2632

2733

2834
2.9.1.dev1

_pytest/python.py

Lines changed: 14 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
@@ -155,14 +156,21 @@ def fixture(scope="function", params=None, autouse=False, ids=None):
155156
so that they are part of the test id. If no ids are provided
156157
they will be generated automatically from the params.
157158
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>')``.
158166
"""
159167
if callable(scope) and params is None and autouse == False:
160168
# direct decoration
161169
return FixtureFunctionMarker(
162-
"function", params, autouse)(scope)
170+
"function", params, autouse, name=name)(scope)
163171
if params is not None and not isinstance(params, (list, tuple)):
164172
params = list(params)
165-
return FixtureFunctionMarker(scope, params, autouse, ids=ids)
173+
return FixtureFunctionMarker(scope, params, autouse, ids=ids, name=name)
166174

167175
def yield_fixture(scope="function", params=None, autouse=False, ids=None):
168176
""" (return a) decorator to mark a yield-fixture factory function
@@ -1989,6 +1997,8 @@ def parsefactories(self, node_or_obj, nodeid=NOTSET, unittest=False):
19891997
# fixture attribute
19901998
continue
19911999
else:
2000+
if marker.name:
2001+
name = marker.name
19922002
assert not name.startswith(self._argprefix)
19932003
fixturedef = FixtureDef(self, nodeid, name, obj,
19942004
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)