Skip to content

Support usefixtures with parametrize #11298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Ahn Ki-Wook
Akhilesh Ramakrishnan
Akiomi Kamakura
Alan Velasco
Aleksandr Brodin
Alessio Izzo
Alex Jones
Alex Lambson
Expand Down
1 change: 1 addition & 0 deletions changelog/4112.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support use `pytest.mark.usefixtures` with `pytest.mark.parametrize`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An example would be cool here I think. Also mentioning this technique in the docs somewhere would be good.

1 change: 1 addition & 0 deletions src/_pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def getfuncargnames(
and not isinstance(
inspect.getattr_static(cls, name, default=None), staticmethod
)
and not hasattr(function, "__self__")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameterized tests are bound to an instance of the test class. And in their signature there is no longer a parameter for an instance of this class ('self').
A check on bound is needed so as not to delete the first fixture or parameter that is passed through arguments.

):
arg_names = arg_names[1:]
# Remove any names that will be replaced with mocks.
Expand Down
8 changes: 6 additions & 2 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -1799,9 +1799,13 @@
if keywords:
self.keywords.update(keywords)

fm = self.session._fixturemanager
fixtureinfo_ = fm.getfixtureinfo(self, self.obj, self.cls)
if fixtureinfo is None:
fm = self.session._fixturemanager
fixtureinfo = fm.getfixtureinfo(self, self.obj, self.cls)
fixtureinfo = fixtureinfo_
elif set(fixtureinfo_.names_closure) != set(fixtureinfo.names_closure):
fixtureinfo_.name2fixturedefs.update(fixtureinfo.name2fixturedefs)
fixtureinfo = fixtureinfo_

Check warning on line 1808 in src/_pytest/python.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/python.py#L1807-L1808

Added lines #L1807 - L1808 were not covered by tests
self._fixtureinfo: FuncFixtureInfo = fixtureinfo
self.fixturenames = fixtureinfo.names_closure
self._initrequest()
Expand Down
28 changes: 28 additions & 0 deletions testing/python/metafunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2112,3 +2112,31 @@ def test_converted_to_str(a, b):
"*= 6 passed in *",
]
)

def test_simple_usefixtures_single_argname(self, pytester: Pytester) -> None:
pytester.makepyfile(
"""
import pytest

@pytest.fixture
def some_fixture():
pytest.skip()

@pytest.mark.parametrize("val", [
1,
pytest.param(2, marks=pytest.mark.usefixtures('some_fixture')),
3,
])
def test_foo(request, val):
assert val
"""
)
result = pytester.runpytest("-vv", "-s")
result.assert_outcomes(passed=2, skipped=1)
result.stdout.fnmatch_lines(
[
"test_simple_usefixtures_single_argname.py::test_foo[1] PASSED",
"test_simple_usefixtures_single_argname.py::test_foo[2] SKIPPED",
"test_simple_usefixtures_single_argname.py::test_foo[3] PASSED",
]
)