diff --git a/AUTHORS b/AUTHORS index 303d04133cb..6399461ee59 100644 --- a/AUTHORS +++ b/AUTHORS @@ -103,6 +103,7 @@ Cornelius Riemenschneider CrazyMerlyn Cristian Vera Cyrus Maden +Daara Shaw Damian Skrzypczak Daniel Grana Daniel Hahler @@ -209,6 +210,7 @@ Jeff Rackauckas Jeff Widman Jenni Rinker Jens Tröger +Jiajun Xu John Eddie Ayson John Litborn John Towler diff --git a/changelog/12426.improvement.rst b/changelog/12426.improvement.rst new file mode 100644 index 00000000000..0da1f838aea --- /dev/null +++ b/changelog/12426.improvement.rst @@ -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. diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 01707418755..4c6c98dd909 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -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): @@ -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( diff --git a/testing/plugins_integration/pytest.ini b/testing/plugins_integration/pytest.ini index 3bacdef62ab..86058fbbac8 100644 --- a/testing/plugins_integration/pytest.ini +++ b/testing/plugins_integration/pytest.ini @@ -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 diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index c939b221f22..cac00ab5401 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -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( """