Skip to content

Commit d61f83c

Browse files
authored
Merge pull request #10698 from pytest-dev/backport-10696-to-7.2.x
[7.2.x] Fix fixtures named teardown being considered by nose
2 parents 4b83a05 + 432a60b commit d61f83c

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ Thomas Grainger
344344
Thomas Hisch
345345
Tim Hoffmann
346346
Tim Strazny
347+
TJ Bruno
347348
Tobias Diez
348349
Tom Dalton
349350
Tom Viner

changelog/10597.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug where a fixture method named ``teardown`` would be called as part of ``nose`` teardown stage.

src/_pytest/python.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ def _inject_setup_class_fixture(self) -> None:
848848
other fixtures (#517).
849849
"""
850850
setup_class = _get_first_non_fixture_func(self.obj, ("setup_class",))
851-
teardown_class = getattr(self.obj, "teardown_class", None)
851+
teardown_class = _get_first_non_fixture_func(self.obj, ("teardown_class",))
852852
if setup_class is None and teardown_class is None:
853853
return
854854

@@ -885,12 +885,12 @@ def _inject_setup_method_fixture(self) -> None:
885885
emit_nose_setup_warning = True
886886
setup_method = _get_first_non_fixture_func(self.obj, (setup_name,))
887887
teardown_name = "teardown_method"
888-
teardown_method = getattr(self.obj, teardown_name, None)
888+
teardown_method = _get_first_non_fixture_func(self.obj, (teardown_name,))
889889
emit_nose_teardown_warning = False
890890
if teardown_method is None and has_nose:
891891
teardown_name = "teardown"
892892
emit_nose_teardown_warning = True
893-
teardown_method = getattr(self.obj, teardown_name, None)
893+
teardown_method = _get_first_non_fixture_func(self.obj, (teardown_name,))
894894
if setup_method is None and teardown_method is None:
895895
return
896896

testing/test_nose.py

+21
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,24 @@ def test_it():
496496
)
497497
result = pytester.runpytest(p, "-p", "nose")
498498
assert result.ret == 0
499+
500+
501+
@pytest.mark.parametrize("fixture_name", ("teardown", "teardown_class"))
502+
def test_teardown_fixture_not_called_directly(fixture_name, pytester: Pytester) -> None:
503+
"""Regression test for #10597."""
504+
p = pytester.makepyfile(
505+
f"""
506+
import pytest
507+
508+
class TestHello:
509+
510+
@pytest.fixture
511+
def {fixture_name}(self):
512+
yield
513+
514+
def test_hello(self, {fixture_name}):
515+
assert True
516+
"""
517+
)
518+
result = pytester.runpytest(p, "-p", "nose")
519+
assert result.ret == 0

0 commit comments

Comments
 (0)