From 2ee8f79f2e56d1d309e8d15170f73a06a589ee3f Mon Sep 17 00:00:00 2001 From: TJ Bruno Date: Thu, 26 Jan 2023 14:57:39 -0800 Subject: [PATCH 1/3] Bugfix nose teardown regression --- AUTHORS | 1 + changelog/10597.bugfix.rst | 1 + src/_pytest/python.py | 6 +++--- 3 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 changelog/10597.bugfix.rst diff --git a/AUTHORS b/AUTHORS index ceb19559e50..2f50a295065 100644 --- a/AUTHORS +++ b/AUTHORS @@ -348,6 +348,7 @@ Thomas Grainger Thomas Hisch Tim Hoffmann Tim Strazny +TJ Bruno Tobias Diez Tom Dalton Tom Viner diff --git a/changelog/10597.bugfix.rst b/changelog/10597.bugfix.rst new file mode 100644 index 00000000000..4ee69133693 --- /dev/null +++ b/changelog/10597.bugfix.rst @@ -0,0 +1 @@ +Fix nose teardown fixture regressions. diff --git a/src/_pytest/python.py b/src/_pytest/python.py index b24a3803eb8..62c31b0f4a3 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -847,7 +847,7 @@ def _inject_setup_class_fixture(self) -> None: other fixtures (#517). """ setup_class = _get_first_non_fixture_func(self.obj, ("setup_class",)) - teardown_class = getattr(self.obj, "teardown_class", None) + teardown_class = _get_first_non_fixture_func(self.obj, ("teardown_class",)) if setup_class is None and teardown_class is None: return @@ -884,12 +884,12 @@ def _inject_setup_method_fixture(self) -> None: emit_nose_setup_warning = True setup_method = _get_first_non_fixture_func(self.obj, (setup_name,)) teardown_name = "teardown_method" - teardown_method = getattr(self.obj, teardown_name, None) + teardown_method = _get_first_non_fixture_func(self.obj, (teardown_name,)) emit_nose_teardown_warning = False if teardown_method is None and has_nose: teardown_name = "teardown" emit_nose_teardown_warning = True - teardown_method = getattr(self.obj, teardown_name, None) + teardown_method = _get_first_non_fixture_func(self.obj, (teardown_name,)) if setup_method is None and teardown_method is None: return From 6911fcddc74ba53490c09da124c2b2efd5f44ae4 Mon Sep 17 00:00:00 2001 From: Teejay Date: Fri, 27 Jan 2023 08:01:37 -0800 Subject: [PATCH 2/3] PR feedback Add a more descriptive changelog entry. Co-authored-by: Bruno Oliveira --- changelog/10597.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/10597.bugfix.rst b/changelog/10597.bugfix.rst index 4ee69133693..9070e74a310 100644 --- a/changelog/10597.bugfix.rst +++ b/changelog/10597.bugfix.rst @@ -1 +1 @@ -Fix nose teardown fixture regressions. +Fix bug where a fixture method named ``teardown`` would be called as part of ``nose`` teardown stage. From 06cfd00c9a20eef0b9e8c54a76497353db2deb72 Mon Sep 17 00:00:00 2001 From: TJ Bruno Date: Fri, 27 Jan 2023 08:27:43 -0800 Subject: [PATCH 3/3] Add test coverage --- testing/test_nose.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/testing/test_nose.py b/testing/test_nose.py index 92d6b95fd87..e838e79ddd5 100644 --- a/testing/test_nose.py +++ b/testing/test_nose.py @@ -496,3 +496,24 @@ def test_it(): ) result = pytester.runpytest(p, "-p", "nose") assert result.ret == 0 + + +@pytest.mark.parametrize("fixture_name", ("teardown", "teardown_class")) +def test_teardown_fixture_not_called_directly(fixture_name, pytester: Pytester) -> None: + """Regression test for #10597.""" + p = pytester.makepyfile( + f""" + import pytest + + class TestHello: + + @pytest.fixture + def {fixture_name}(self): + yield + + def test_hello(self, {fixture_name}): + assert True + """ + ) + result = pytester.runpytest(p, "-p", "nose") + assert result.ret == 0