From 59b16394eaaa25626fa8da9294b055f6309f4ead Mon Sep 17 00:00:00 2001 From: Jiajun Xu Date: Fri, 7 Jun 2024 23:56:55 -0400 Subject: [PATCH 01/15] warns when usefixtures is empty --- src/_pytest/fixtures.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 5817e88f47d..4feca1009c5 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -1567,6 +1567,8 @@ 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"): + if not mark.args: + warnings.warn(f"Warning: empty usefixtures in {node.name}.") yield from mark.args def getfixtureclosure( From 00ad5ea97046cdafa0c036ceffc5813ef6362309 Mon Sep 17 00:00:00 2001 From: Jiajun Xu Date: Wed, 10 Jul 2024 13:52:34 -0400 Subject: [PATCH 02/15] Changed _getusefixturesnames to include location - Updated _getusefixturesnames to iterate with node and marker using node.iter_markers_with_node. - Modified warning message to include the location of the marker node. - Replaced warnings.warn with node.warn to issue warnings. --- src/_pytest/fixtures.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 4feca1009c5..bad145e44a9 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -1566,9 +1566,10 @@ 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: - warnings.warn(f"Warning: empty usefixtures in {node.name}.") + location = getattr(marker_node, "location", "unknown location") + node.warn(Warning(f"empty usefixtures in {node.name} from {location}.")) yield from mark.args def getfixtureclosure( From e33c666f4119ece86de8f628b23c20a5af605c8d Mon Sep 17 00:00:00 2001 From: Jiajun Xu Date: Wed, 10 Jul 2024 18:01:18 -0400 Subject: [PATCH 03/15] Updated changelog to include fixture improvement --- changelog/12426.improvement.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/12426.improvement.rst diff --git a/changelog/12426.improvement.rst b/changelog/12426.improvement.rst new file mode 100644 index 00000000000..70e0ac2ec5f --- /dev/null +++ b/changelog/12426.improvement.rst @@ -0,0 +1 @@ +A warning in now issued when @pytest.mark.usefixtures() is used without specifying any fixtures. Previously, empty usefixtures markers were silently ignored. \ No newline at end of file From 916f9ab3ad0b09fe585be9cbfe2a707786a83dd4 Mon Sep 17 00:00:00 2001 From: Jiajun Xu <131632195+JiajunXu2@users.noreply.github.com> Date: Wed, 10 Jul 2024 20:24:05 -0400 Subject: [PATCH 04/15] Update 12426.improvement.rst fixed typo --- changelog/12426.improvement.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/12426.improvement.rst b/changelog/12426.improvement.rst index 70e0ac2ec5f..dbc47fa60d7 100644 --- a/changelog/12426.improvement.rst +++ b/changelog/12426.improvement.rst @@ -1 +1 @@ -A warning in now issued when @pytest.mark.usefixtures() is used without specifying any fixtures. Previously, empty usefixtures markers were silently ignored. \ No newline at end of file +A warning is now issued when @pytest.mark.usefixtures() is used without specifying any fixtures. Previously, empty usefixtures markers were silently ignored. From ec6bc0ee6af013be329ed67e0a3295c0428c2caf Mon Sep 17 00:00:00 2001 From: Jiajun Xu <131632195+JiajunXu2@users.noreply.github.com> Date: Mon, 15 Jul 2024 09:16:22 -0400 Subject: [PATCH 05/15] Update changelog/12426.improvement.rst Co-authored-by: Bruno Oliveira --- changelog/12426.improvement.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/12426.improvement.rst b/changelog/12426.improvement.rst index dbc47fa60d7..0da1f838aea 100644 --- a/changelog/12426.improvement.rst +++ b/changelog/12426.improvement.rst @@ -1 +1 @@ -A warning is now issued when @pytest.mark.usefixtures() is used without specifying any fixtures. Previously, empty usefixtures markers were silently ignored. +A warning is now issued when :ref:`pytest.mark.usefixtures ref` is used without specifying any fixtures. Previously, empty usefixtures markers were silently ignored. From 1eb63d6583a175d4c60312528d58155021257b54 Mon Sep 17 00:00:00 2001 From: Jiajun Xu <131632195+JiajunXu2@users.noreply.github.com> Date: Mon, 15 Jul 2024 09:16:39 -0400 Subject: [PATCH 06/15] Update src/_pytest/fixtures.py Co-authored-by: Bruno Oliveira --- src/_pytest/fixtures.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index bad145e44a9..a623bc23cfd 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -1569,7 +1569,7 @@ def _getusefixturesnames(self, node: nodes.Item) -> Iterator[str]: for marker_node, mark in node.iter_markers_with_node(name="usefixtures"): if not mark.args: location = getattr(marker_node, "location", "unknown location") - node.warn(Warning(f"empty usefixtures in {node.name} from {location}.")) + node.warn(Warning(f"usefixtures() is empty, so it has no effect")) yield from mark.args def getfixtureclosure( From 9da1a3134f3434c4e88c34516eb9fc06dff3d27e Mon Sep 17 00:00:00 2001 From: Jiajun Xu Date: Mon, 15 Jul 2024 12:39:14 -0400 Subject: [PATCH 07/15] Changed node.warn to marker_node.warn --- src/_pytest/fixtures.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index a623bc23cfd..99d43b3a01f 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -1568,8 +1568,7 @@ def _getusefixturesnames(self, node: nodes.Item) -> Iterator[str]: """Return the names of usefixtures fixtures applicable to node.""" for marker_node, mark in node.iter_markers_with_node(name="usefixtures"): if not mark.args: - location = getattr(marker_node, "location", "unknown location") - node.warn(Warning(f"usefixtures() is empty, so it has no effect")) + marker_node.warn(Warning("usefixtures() is empty, so it has no effect")) yield from mark.args def getfixtureclosure( From 64a3a22fb00508998bf2f64f3536ff2495c6a6b3 Mon Sep 17 00:00:00 2001 From: Daara Shaw Date: Thu, 28 Nov 2024 20:09:07 +0000 Subject: [PATCH 08/15] Add test for empty usefixtures mark --- testing/python/fixtures.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index c939b221f22..6476ad44431 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -1435,6 +1435,19 @@ def test_two(self): reprec = pytester.inline_run() reprec.assertoutcome(passed=2) + def test_empty_usefixtures_marker(self, pytester: Pytester) -> None: + pytester.makepyfile( + """ + import pytest + + @pytest.mark.usefixtures() + def test_one(): + assert 1 == 1 + """ + ) + reprec = pytester.inline_run() + reprec.assertoutcome(failed=1) + def test_usefixtures_ini(self, pytester: Pytester) -> None: pytester.makeini( """ From 4a0a0226627a45cfd7fd4dba9666a579024b711b Mon Sep 17 00:00:00 2001 From: Daara Shaw Date: Thu, 28 Nov 2024 20:16:38 +0000 Subject: [PATCH 09/15] Update AUTHORS --- AUTHORS | 2 ++ 1 file changed, 2 insertions(+) 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 From b3296654d8b9a245433150fc64d9e689fc118dd2 Mon Sep 17 00:00:00 2001 From: Daara Shaw Date: Fri, 29 Nov 2024 18:06:02 +0000 Subject: [PATCH 10/15] Change test to check warning is raised --- testing/python/fixtures.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index 6476ad44431..3d5b1d3a831 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -1445,8 +1445,10 @@ def test_one(): assert 1 == 1 """ ) - reprec = pytester.inline_run() - reprec.assertoutcome(failed=1) + result = pytester.runpytest() + result.stdout.fnmatch_lines( + "*Warning: usefixtures() is empty, so it has no effect" + ) def test_usefixtures_ini(self, pytester: Pytester) -> None: pytester.makeini( From 0b51f91436ce902e3dc48f54dcdcc4b9ec66c2af Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 29 Nov 2024 19:08:03 -0300 Subject: [PATCH 11/15] Use PytestWarning and tweak the message --- src/_pytest/fixtures.py | 5 ++++- testing/python/fixtures.py | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 5a83ae16687..cf762f4b7a0 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): @@ -1564,7 +1565,9 @@ def _getusefixturesnames(self, node: nodes.Item) -> Iterator[str]: """Return the names of usefixtures fixtures applicable to node.""" for marker_node, mark in node.iter_markers_with_node(name="usefixtures"): if not mark.args: - marker_node.warn(Warning("usefixtures() is empty, so it has no effect")) + marker_node.warn( + PytestWarning("usefixtures() without arguments has no effect") + ) yield from mark.args def getfixtureclosure( diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index 3d5b1d3a831..90df9bd9db4 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -1436,6 +1436,7 @@ def test_two(self): reprec.assertoutcome(passed=2) def test_empty_usefixtures_marker(self, pytester: Pytester) -> None: + """Empty usefixtures() marker issues a warning (#12439).""" pytester.makepyfile( """ import pytest @@ -1447,7 +1448,7 @@ def test_one(): ) result = pytester.runpytest() result.stdout.fnmatch_lines( - "*Warning: usefixtures() is empty, so it has no effect" + "*PytestWarning: usefixtures() without arguments has no effect" ) def test_usefixtures_ini(self, pytester: Pytester) -> None: From 1b6b836c33073030ff4636ed48c4afaf256b218e Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 29 Nov 2024 17:40:23 -0300 Subject: [PATCH 12/15] Improve warning message to indicate the node id --- src/_pytest/fixtures.py | 4 +++- testing/python/fixtures.py | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index cf762f4b7a0..4c6c98dd909 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -1566,7 +1566,9 @@ def _getusefixturesnames(self, node: nodes.Item) -> Iterator[str]: for marker_node, mark in node.iter_markers_with_node(name="usefixtures"): if not mark.args: marker_node.warn( - PytestWarning("usefixtures() without arguments has no effect") + PytestWarning( + f"usefixtures() in {node.nodeid} without arguments has no effect" + ) ) yield from mark.args diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index 90df9bd9db4..cac00ab5401 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -1448,7 +1448,8 @@ def test_one(): ) result = pytester.runpytest() result.stdout.fnmatch_lines( - "*PytestWarning: usefixtures() without arguments has no effect" + "*PytestWarning: usefixtures() in test_empty_usefixtures_marker.py::test_one" + " without arguments has no effect" ) def test_usefixtures_ini(self, pytester: Pytester) -> None: From f8a8666a212824e5b84d2041fe576571b74794ff Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 29 Nov 2024 17:40:47 -0300 Subject: [PATCH 13/15] Ignore warning about usefixtures() empty in plugins integration Currently failing due to pytest-bdd --- testing/plugins_integration/pytest.ini | 1 + 1 file changed, 1 insertion(+) 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 From 1a295ca867f9cbef7fccc16688dc0f8f2021656c Mon Sep 17 00:00:00 2001 From: Daara Shaw Date: Sat, 30 Nov 2024 13:19:33 +0000 Subject: [PATCH 14/15] Revert "Ignore warning about usefixtures() empty in plugins integration" This reverts commit f8a8666a212824e5b84d2041fe576571b74794ff. Fix merged into pytest-bdd --- testing/plugins_integration/pytest.ini | 1 - 1 file changed, 1 deletion(-) diff --git a/testing/plugins_integration/pytest.ini b/testing/plugins_integration/pytest.ini index 86058fbbac8..3bacdef62ab 100644 --- a/testing/plugins_integration/pytest.ini +++ b/testing/plugins_integration/pytest.ini @@ -3,5 +3,4 @@ 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 From 858d237b92f753897d5fefa38f63942a274e7d53 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 29 Nov 2024 17:40:47 -0300 Subject: [PATCH 15/15] Ignore warning about usefixtures() empty in plugins integration Currently failing due to pytest-bdd --- testing/plugins_integration/pytest.ini | 1 + 1 file changed, 1 insertion(+) 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