Skip to content

Commit 374c432

Browse files
refactor finding xunit setup/teardown functions
s/_get_non_fixture_func(obj, name: str)/_get_first_non_fixture_func(obj, names: List[str])/
1 parent 2c071a0 commit 374c432

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

src/_pytest/python.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,12 @@ def _inject_setup_module_fixture(self):
438438
Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with
439439
other fixtures (#517).
440440
"""
441-
setup_module = _get_non_fixture_func(self.obj, "setUpModule")
442-
if setup_module is None:
443-
setup_module = _get_non_fixture_func(self.obj, "setup_module")
444-
445-
teardown_module = _get_non_fixture_func(self.obj, "tearDownModule")
446-
if teardown_module is None:
447-
teardown_module = _get_non_fixture_func(self.obj, "teardown_module")
441+
setup_module = _get_first_non_fixture_func(
442+
self.obj, ("setUpModule", "setup_module")
443+
)
444+
teardown_module = _get_first_non_fixture_func(
445+
self.obj, ("tearDownModule", "teardown_module")
446+
)
448447

449448
if setup_module is None and teardown_module is None:
450449
return
@@ -466,8 +465,10 @@ def _inject_setup_function_fixture(self):
466465
Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with
467466
other fixtures (#517).
468467
"""
469-
setup_function = _get_non_fixture_func(self.obj, "setup_function")
470-
teardown_function = _get_non_fixture_func(self.obj, "teardown_function")
468+
setup_function = _get_first_non_fixture_func(self.obj, ("setup_function",))
469+
teardown_function = _get_first_non_fixture_func(
470+
self.obj, ("teardown_function",)
471+
)
471472
if setup_function is None and teardown_function is None:
472473
return
473474

@@ -551,15 +552,15 @@ def __init__(self, fspath, parent=None, config=None, session=None, nodeid=None):
551552
def setup(self):
552553
# not using fixtures to call setup_module here because autouse fixtures
553554
# from packages are not called automatically (#4085)
554-
setup_module = _get_non_fixture_func(self.obj, "setUpModule")
555-
if setup_module is None:
556-
setup_module = _get_non_fixture_func(self.obj, "setup_module")
555+
setup_module = _get_first_non_fixture_func(
556+
self.obj, ("setUpModule", "setup_module")
557+
)
557558
if setup_module is not None:
558559
_call_with_optional_argument(setup_module, self.obj)
559560

560-
teardown_module = _get_non_fixture_func(self.obj, "tearDownModule")
561-
if teardown_module is None:
562-
teardown_module = _get_non_fixture_func(self.obj, "teardown_module")
561+
teardown_module = _get_first_non_fixture_func(
562+
self.obj, ("tearDownModule", "teardown_module")
563+
)
563564
if teardown_module is not None:
564565
func = partial(_call_with_optional_argument, teardown_module, self.obj)
565566
self.addfinalizer(func)
@@ -662,14 +663,15 @@ def _call_with_optional_argument(func, arg):
662663
func()
663664

664665

665-
def _get_non_fixture_func(obj, name):
666+
def _get_first_non_fixture_func(obj, names):
666667
"""Return the attribute from the given object to be used as a setup/teardown
667668
xunit-style function, but only if not marked as a fixture to
668669
avoid calling it twice.
669670
"""
670-
meth = getattr(obj, name, None)
671-
if fixtures.getfixturemarker(meth) is None:
672-
return meth
671+
for name in names:
672+
meth = getattr(obj, name, None)
673+
if meth is not None and fixtures.getfixturemarker(meth) is None:
674+
return meth
673675

674676

675677
class Class(PyCollector):
@@ -709,7 +711,7 @@ def _inject_setup_class_fixture(self):
709711
Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with
710712
other fixtures (#517).
711713
"""
712-
setup_class = _get_non_fixture_func(self.obj, "setup_class")
714+
setup_class = _get_first_non_fixture_func(self.obj, ("setup_class",))
713715
teardown_class = getattr(self.obj, "teardown_class", None)
714716
if setup_class is None and teardown_class is None:
715717
return
@@ -733,7 +735,7 @@ def _inject_setup_method_fixture(self):
733735
Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with
734736
other fixtures (#517).
735737
"""
736-
setup_method = _get_non_fixture_func(self.obj, "setup_method")
738+
setup_method = _get_first_non_fixture_func(self.obj, ("setup_method",))
737739
teardown_method = getattr(self.obj, "teardown_method", None)
738740
if setup_method is None and teardown_method is None:
739741
return

0 commit comments

Comments
 (0)