Prohibit pytest.mark.usefixtures in pytest.param (#12828)

Mitigates #4112 a bit.

Co-authored-by: Bruno Oliveira <bruno@soliv.dev>
This commit is contained in:
Christopher Head 2024-10-09 12:13:56 -07:00 committed by GitHub
parent fb740251fe
commit 23256dd967
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 30 additions and 1 deletions

View File

@ -95,6 +95,7 @@ Christine Mecklenborg
Christoph Buelter
Christopher Dignam
Christopher Gilling
Christopher Head
Claire Cecil
Claudio Madotto
Clément M.T. Robert

View File

@ -0,0 +1 @@
Using :ref:`pytest.mark.usefixtures <pytest.mark.usefixtures ref>` on :func:`pytest.param` now produces an error instead of silently doing nothing.

View File

@ -66,7 +66,12 @@ def param(
assert eval(test_input) == expected
:param values: Variable args of the values of the parameter set, in order.
:param marks: A single mark or a list of marks to be applied to this parameter set.
:param marks:
A single mark or a list of marks to be applied to this parameter set.
:ref:`pytest.mark.usefixtures <pytest.mark.usefixtures ref>` cannot be added via this parameter.
:param id: The id to attribute to this parameter set.
"""
return ParameterSet.param(*values, marks=marks, id=id)

View File

@ -88,6 +88,11 @@ class ParameterSet(NamedTuple):
marks = (marks,)
else:
assert isinstance(marks, collections.abc.Collection)
if any(i.name == "usefixtures" for i in marks):
raise ValueError(
"pytest.param cannot add pytest.mark.usefixtures; see "
"https://docs.pytest.org/en/stable/reference/reference.html#pytest-param"
)
if id is not None:
if not isinstance(id, str):

View File

@ -407,6 +407,23 @@ class TestParameterize:
res = pytester.runpytest("--collect-only")
res.stdout.fnmatch_lines(["*spam-2*", "*ham-2*"])
def test_param_rejects_usefixtures(self, pytester: Pytester) -> None:
pytester.makepyfile(
"""
import pytest
@pytest.mark.parametrize("x", [
pytest.param(1, marks=[pytest.mark.usefixtures("foo")]),
])
def test_foo(x):
pass
"""
)
res = pytester.runpytest("--collect-only")
res.stdout.fnmatch_lines(
["*test_param_rejects_usefixtures.py:4*", "*pytest.param(*"]
)
def test_function_instance(pytester: Pytester) -> None:
items = pytester.getitems(