@@ -438,13 +438,12 @@ def _inject_setup_module_fixture(self):
438
438
Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with
439
439
other fixtures (#517).
440
440
"""
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
+ )
448
447
449
448
if setup_module is None and teardown_module is None :
450
449
return
@@ -466,8 +465,10 @@ def _inject_setup_function_fixture(self):
466
465
Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with
467
466
other fixtures (#517).
468
467
"""
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
+ )
471
472
if setup_function is None and teardown_function is None :
472
473
return
473
474
@@ -551,15 +552,15 @@ def __init__(self, fspath, parent=None, config=None, session=None, nodeid=None):
551
552
def setup (self ):
552
553
# not using fixtures to call setup_module here because autouse fixtures
553
554
# 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
+ )
557
558
if setup_module is not None :
558
559
_call_with_optional_argument (setup_module , self .obj )
559
560
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
+ )
563
564
if teardown_module is not None :
564
565
func = partial (_call_with_optional_argument , teardown_module , self .obj )
565
566
self .addfinalizer (func )
@@ -662,14 +663,15 @@ def _call_with_optional_argument(func, arg):
662
663
func ()
663
664
664
665
665
- def _get_non_fixture_func (obj , name ):
666
+ def _get_first_non_fixture_func (obj , names ):
666
667
"""Return the attribute from the given object to be used as a setup/teardown
667
668
xunit-style function, but only if not marked as a fixture to
668
669
avoid calling it twice.
669
670
"""
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
673
675
674
676
675
677
class Class (PyCollector ):
@@ -709,7 +711,7 @@ def _inject_setup_class_fixture(self):
709
711
Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with
710
712
other fixtures (#517).
711
713
"""
712
- setup_class = _get_non_fixture_func (self .obj , "setup_class" )
714
+ setup_class = _get_first_non_fixture_func (self .obj , ( "setup_class" ,) )
713
715
teardown_class = getattr (self .obj , "teardown_class" , None )
714
716
if setup_class is None and teardown_class is None :
715
717
return
@@ -733,7 +735,7 @@ def _inject_setup_method_fixture(self):
733
735
Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with
734
736
other fixtures (#517).
735
737
"""
736
- setup_method = _get_non_fixture_func (self .obj , "setup_method" )
738
+ setup_method = _get_first_non_fixture_func (self .obj , ( "setup_method" ,) )
737
739
teardown_method = getattr (self .obj , "teardown_method" , None )
738
740
if setup_method is None and teardown_method is None :
739
741
return
0 commit comments