Skip to content

Commit 0551453

Browse files
committed
support usefixtures with parametrize
1 parent 194a782 commit 0551453

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/_pytest/python.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,14 +505,26 @@ def _genfunctions(self, name: str, funcobj) -> Iterator["Function"]:
505505

506506
for callspec in metafunc._calls:
507507
subname = f"{name}[{callspec.id}]"
508-
yield Function.from_parent(
508+
node = Function.from_parent(
509509
self,
510510
name=subname,
511511
callspec=callspec,
512512
fixtureinfo=fixtureinfo,
513513
keywords={callspec.id: True},
514514
originalname=name,
515515
)
516+
# if usefixtures is added via a parameter, then there will be
517+
# fixtures missing from the node.fixturenames
518+
callspec_usefixtures = tuple(
519+
arg
520+
for mark in node.iter_markers(name="usefixtures")
521+
for arg in mark.args
522+
if arg not in node.fixturenames
523+
)
524+
if callspec_usefixtures:
525+
# node.fixturenames must be unique for this parameter
526+
node.fixturenames = [*node.fixturenames, *callspec_usefixtures]
527+
yield node
516528

517529

518530
def importtestmodule(

testing/python/metafunc.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,3 +2112,31 @@ def test_converted_to_str(a, b):
21122112
"*= 6 passed in *",
21132113
]
21142114
)
2115+
2116+
def test_simple_usefixtures_single_argname(self, pytester: Pytester) -> None:
2117+
pytester.makepyfile(
2118+
"""
2119+
import pytest
2120+
2121+
@pytest.fixture
2122+
def some_fixture():
2123+
pytest.skip()
2124+
2125+
@pytest.mark.parametrize("val", [
2126+
1,
2127+
pytest.param(2, marks=pytest.mark.usefixtures('some_fixture')),
2128+
3,
2129+
])
2130+
def test_foo(request, val):
2131+
assert val
2132+
"""
2133+
)
2134+
result = pytester.runpytest("-vv", "-s")
2135+
result.assert_outcomes(passed=2, skipped=1)
2136+
result.stdout.fnmatch_lines(
2137+
[
2138+
"test_simple_usefixtures_single_argname.py::test_foo[1] PASSED",
2139+
"test_simple_usefixtures_single_argname.py::test_foo[2] SKIPPED",
2140+
"test_simple_usefixtures_single_argname.py::test_foo[3] PASSED",
2141+
]
2142+
)

0 commit comments

Comments
 (0)