From 70c7273640d58c48e7a926e5beba9b087f6f5136 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 3 Oct 2018 18:50:14 -0300 Subject: [PATCH] Fix request.fixturenames to return fixtures created dynamically Fix #3057 --- changelog/3057.bugfix.rst | 1 + src/_pytest/fixtures.py | 6 ++++-- .../fixtures/test_getfixturevalue_dynamic.py | 20 +++++++++++++++++++ testing/python/fixture.py | 6 ++++++ 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 changelog/3057.bugfix.rst create mode 100644 testing/example_scripts/fixtures/test_getfixturevalue_dynamic.py diff --git a/changelog/3057.bugfix.rst b/changelog/3057.bugfix.rst new file mode 100644 index 000000000..8cc22f278 --- /dev/null +++ b/changelog/3057.bugfix.rst @@ -0,0 +1 @@ +``request.fixturenames`` now correctly returns the name of fixtures created by ``request.getfixturevalue()``. diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 068e6814c..b8b6706aa 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -359,8 +359,10 @@ class FixtureRequest(FuncargnamesCompatAttr): @property def fixturenames(self): - # backward incompatible note: now a readonly property - return list(self._pyfuncitem._fixtureinfo.names_closure) + """names of all active fixtures in this request""" + result = list(self._pyfuncitem._fixtureinfo.names_closure) + result.extend(set(self._fixture_defs).difference(result)) + return result @property def node(self): diff --git a/testing/example_scripts/fixtures/test_getfixturevalue_dynamic.py b/testing/example_scripts/fixtures/test_getfixturevalue_dynamic.py new file mode 100644 index 000000000..055a1220b --- /dev/null +++ b/testing/example_scripts/fixtures/test_getfixturevalue_dynamic.py @@ -0,0 +1,20 @@ +import pytest + + +@pytest.fixture +def dynamic(): + pass + + +@pytest.fixture +def a(request): + request.getfixturevalue("dynamic") + + +@pytest.fixture +def b(a): + pass + + +def test(b, request): + assert request.fixturenames == ["b", "request", "a", "dynamic"] diff --git a/testing/python/fixture.py b/testing/python/fixture.py index 33c040e31..06bc7fb21 100644 --- a/testing/python/fixture.py +++ b/testing/python/fixture.py @@ -756,6 +756,12 @@ class TestRequestBasic(object): reprec = testdir.inline_run() reprec.assertoutcome(passed=1) + def test_request_fixturenames_dynamic_fixture(self, testdir): + """Regression test for #3057""" + testdir.copy_example("fixtures/test_getfixturevalue_dynamic.py") + result = testdir.runpytest() + result.stdout.fnmatch_lines("*1 passed*") + def test_funcargnames_compatattr(self, testdir): testdir.makepyfile( """