Skip to content

Commit

Permalink
Warn pytest.mark.usefixtures() is used without arguments (#13008)
Browse files Browse the repository at this point in the history
Closes #12426
Closes #12439

---------

Co-authored-by: Jiajun Xu <[email protected]>
Co-authored-by: Bruno Oliveira <[email protected]>
  • Loading branch information
3 people authored Dec 1, 2024
1 parent 035895e commit b2dce69
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Cornelius Riemenschneider
CrazyMerlyn
Cristian Vera
Cyrus Maden
Daara Shaw
Damian Skrzypczak
Daniel Grana
Daniel Hahler
Expand Down Expand Up @@ -209,6 +210,7 @@ Jeff Rackauckas
Jeff Widman
Jenni Rinker
Jens Tröger
Jiajun Xu
John Eddie Ayson
John Litborn
John Towler
Expand Down
1 change: 1 addition & 0 deletions changelog/12426.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +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.
9 changes: 8 additions & 1 deletion src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
from _pytest.scope import HIGH_SCOPES
from _pytest.scope import Scope
from _pytest.warning_types import PytestRemovedIn9Warning
from _pytest.warning_types import PytestWarning


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

def _getusefixturesnames(self, node: nodes.Item) -> Iterator[str]:
"""Return the names of usefixtures fixtures applicable to node."""
for mark in node.iter_markers(name="usefixtures"):
for marker_node, mark in node.iter_markers_with_node(name="usefixtures"):
if not mark.args:
marker_node.warn(
PytestWarning(
f"usefixtures() in {node.nodeid} without arguments has no effect"
)
)
yield from mark.args

def getfixtureclosure(
Expand Down
1 change: 1 addition & 0 deletions testing/plugins_integration/pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ addopts = --strict-markers
asyncio_mode = strict
filterwarnings =
error::pytest.PytestWarning
ignore:usefixtures.* without arguments has no effect:pytest.PytestWarning
ignore:.*.fspath is deprecated and will be replaced by .*.path.*:pytest.PytestDeprecationWarning
17 changes: 17 additions & 0 deletions testing/python/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,23 @@ def test_two(self):
reprec = pytester.inline_run()
reprec.assertoutcome(passed=2)

def test_empty_usefixtures_marker(self, pytester: Pytester) -> None:
"""Empty usefixtures() marker issues a warning (#12439)."""
pytester.makepyfile(
"""
import pytest
@pytest.mark.usefixtures()
def test_one():
assert 1 == 1
"""
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(
"*PytestWarning: usefixtures() in test_empty_usefixtures_marker.py::test_one"
" without arguments has no effect"
)

def test_usefixtures_ini(self, pytester: Pytester) -> None:
pytester.makeini(
"""
Expand Down

0 comments on commit b2dce69

Please sign in to comment.