Skip to content

Commit 23256dd

Browse files
Hawk777nicoddemus
andauthored
Prohibit pytest.mark.usefixtures in pytest.param (#12828)
Mitigates #4112 a bit. Co-authored-by: Bruno Oliveira <[email protected]>
1 parent fb74025 commit 23256dd

File tree

5 files changed

+30
-1
lines changed

5 files changed

+30
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ Christine Mecklenborg
9595
Christoph Buelter
9696
Christopher Dignam
9797
Christopher Gilling
98+
Christopher Head
9899
Claire Cecil
99100
Claudio Madotto
100101
Clément M.T. Robert

changelog/4112.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Using :ref:`pytest.mark.usefixtures <pytest.mark.usefixtures ref>` on :func:`pytest.param` now produces an error instead of silently doing nothing.

src/_pytest/mark/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ def test_eval(test_input, expected):
6666
assert eval(test_input) == expected
6767
6868
:param values: Variable args of the values of the parameter set, in order.
69-
:param marks: A single mark or a list of marks to be applied to this parameter set.
69+
70+
:param marks:
71+
A single mark or a list of marks to be applied to this parameter set.
72+
73+
:ref:`pytest.mark.usefixtures <pytest.mark.usefixtures ref>` cannot be added via this parameter.
74+
7075
:param id: The id to attribute to this parameter set.
7176
"""
7277
return ParameterSet.param(*values, marks=marks, id=id)

src/_pytest/mark/structures.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ def param(
8888
marks = (marks,)
8989
else:
9090
assert isinstance(marks, collections.abc.Collection)
91+
if any(i.name == "usefixtures" for i in marks):
92+
raise ValueError(
93+
"pytest.param cannot add pytest.mark.usefixtures; see "
94+
"https://docs.pytest.org/en/stable/reference/reference.html#pytest-param"
95+
)
9196

9297
if id is not None:
9398
if not isinstance(id, str):

testing/python/integration.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,23 @@ def test_params(a, b):
407407
res = pytester.runpytest("--collect-only")
408408
res.stdout.fnmatch_lines(["*spam-2*", "*ham-2*"])
409409

410+
def test_param_rejects_usefixtures(self, pytester: Pytester) -> None:
411+
pytester.makepyfile(
412+
"""
413+
import pytest
414+
415+
@pytest.mark.parametrize("x", [
416+
pytest.param(1, marks=[pytest.mark.usefixtures("foo")]),
417+
])
418+
def test_foo(x):
419+
pass
420+
"""
421+
)
422+
res = pytester.runpytest("--collect-only")
423+
res.stdout.fnmatch_lines(
424+
["*test_param_rejects_usefixtures.py:4*", "*pytest.param(*"]
425+
)
426+
410427

411428
def test_function_instance(pytester: Pytester) -> None:
412429
items = pytester.getitems(

0 commit comments

Comments
 (0)