Skip to content

Commit b2dce69

Browse files
daria-shawJiajunXu2nicoddemus
authored
Warn pytest.mark.usefixtures() is used without arguments (#13008)
Closes #12426 Closes #12439 --------- Co-authored-by: Jiajun Xu <[email protected]> Co-authored-by: Bruno Oliveira <[email protected]>
1 parent 035895e commit b2dce69

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

AUTHORS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ Cornelius Riemenschneider
103103
CrazyMerlyn
104104
Cristian Vera
105105
Cyrus Maden
106+
Daara Shaw
106107
Damian Skrzypczak
107108
Daniel Grana
108109
Daniel Hahler
@@ -209,6 +210,7 @@ Jeff Rackauckas
209210
Jeff Widman
210211
Jenni Rinker
211212
Jens Tröger
213+
Jiajun Xu
212214
John Eddie Ayson
213215
John Litborn
214216
John Towler

changelog/12426.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A warning is now issued when :ref:`pytest.mark.usefixtures ref` is used without specifying any fixtures. Previously, empty usefixtures markers were silently ignored.

src/_pytest/fixtures.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
from _pytest.scope import HIGH_SCOPES
7272
from _pytest.scope import Scope
7373
from _pytest.warning_types import PytestRemovedIn9Warning
74+
from _pytest.warning_types import PytestWarning
7475

7576

7677
if sys.version_info < (3, 11):
@@ -1562,7 +1563,13 @@ def _getautousenames(self, node: nodes.Node) -> Iterator[str]:
15621563

15631564
def _getusefixturesnames(self, node: nodes.Item) -> Iterator[str]:
15641565
"""Return the names of usefixtures fixtures applicable to node."""
1565-
for mark in node.iter_markers(name="usefixtures"):
1566+
for marker_node, mark in node.iter_markers_with_node(name="usefixtures"):
1567+
if not mark.args:
1568+
marker_node.warn(
1569+
PytestWarning(
1570+
f"usefixtures() in {node.nodeid} without arguments has no effect"
1571+
)
1572+
)
15661573
yield from mark.args
15671574

15681575
def getfixtureclosure(

testing/plugins_integration/pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ addopts = --strict-markers
33
asyncio_mode = strict
44
filterwarnings =
55
error::pytest.PytestWarning
6+
ignore:usefixtures.* without arguments has no effect:pytest.PytestWarning
67
ignore:.*.fspath is deprecated and will be replaced by .*.path.*:pytest.PytestDeprecationWarning

testing/python/fixtures.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,23 @@ def test_two(self):
14351435
reprec = pytester.inline_run()
14361436
reprec.assertoutcome(passed=2)
14371437

1438+
def test_empty_usefixtures_marker(self, pytester: Pytester) -> None:
1439+
"""Empty usefixtures() marker issues a warning (#12439)."""
1440+
pytester.makepyfile(
1441+
"""
1442+
import pytest
1443+
1444+
@pytest.mark.usefixtures()
1445+
def test_one():
1446+
assert 1 == 1
1447+
"""
1448+
)
1449+
result = pytester.runpytest()
1450+
result.stdout.fnmatch_lines(
1451+
"*PytestWarning: usefixtures() in test_empty_usefixtures_marker.py::test_one"
1452+
" without arguments has no effect"
1453+
)
1454+
14381455
def test_usefixtures_ini(self, pytester: Pytester) -> None:
14391456
pytester.makeini(
14401457
"""

0 commit comments

Comments
 (0)