Skip to content

Commit 89ae466

Browse files
committed
support usefixtures with parametrize
1 parent 39f9306 commit 89ae466

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
@@ -507,14 +507,26 @@ def _genfunctions(self, name: str, funcobj) -> Iterator["Function"]:
507507

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

519531

520532
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)