Skip to content

Commit a1208f5

Browse files
authored
Merge pull request #4075 from nicoddemus/dynamic-fixturenames
Fix request.fixturenames to return fixtures created dynamically
2 parents b098292 + 70c7273 commit a1208f5

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

changelog/3057.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``request.fixturenames`` now correctly returns the name of fixtures created by ``request.getfixturevalue()``.

src/_pytest/fixtures.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,10 @@ def __init__(self, pyfuncitem):
359359

360360
@property
361361
def fixturenames(self):
362-
# backward incompatible note: now a readonly property
363-
return list(self._pyfuncitem._fixtureinfo.names_closure)
362+
"""names of all active fixtures in this request"""
363+
result = list(self._pyfuncitem._fixtureinfo.names_closure)
364+
result.extend(set(self._fixture_defs).difference(result))
365+
return result
364366

365367
@property
366368
def node(self):
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def dynamic():
6+
pass
7+
8+
9+
@pytest.fixture
10+
def a(request):
11+
request.getfixturevalue("dynamic")
12+
13+
14+
@pytest.fixture
15+
def b(a):
16+
pass
17+
18+
19+
def test(b, request):
20+
assert request.fixturenames == ["b", "request", "a", "dynamic"]

testing/python/fixture.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,12 @@ def test_function(request, farg):
756756
reprec = testdir.inline_run()
757757
reprec.assertoutcome(passed=1)
758758

759+
def test_request_fixturenames_dynamic_fixture(self, testdir):
760+
"""Regression test for #3057"""
761+
testdir.copy_example("fixtures/test_getfixturevalue_dynamic.py")
762+
result = testdir.runpytest()
763+
result.stdout.fnmatch_lines("*1 passed*")
764+
759765
def test_funcargnames_compatattr(self, testdir):
760766
testdir.makepyfile(
761767
"""

0 commit comments

Comments
 (0)