@@ -582,13 +582,13 @@ def _getobj(self):
582
582
return importtestmodule (self .path , self .config )
583
583
584
584
def collect (self ) -> Iterable [Union [nodes .Item , nodes .Collector ]]:
585
- self ._inject_setup_module_fixture ()
586
- self ._inject_setup_function_fixture ()
585
+ self ._register_setup_module_fixture ()
586
+ self ._register_setup_function_fixture ()
587
587
self .session ._fixturemanager .parsefactories (self )
588
588
return super ().collect ()
589
589
590
- def _inject_setup_module_fixture (self ) -> None :
591
- """Inject a hidden autouse, module scoped fixture into the collected module object
590
+ def _register_setup_module_fixture (self ) -> None :
591
+ """Register an autouse, module- scoped fixture for the collected module object
592
592
that invokes setUpModule/tearDownModule if either or both are available.
593
593
594
594
Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with
@@ -604,23 +604,25 @@ def _inject_setup_module_fixture(self) -> None:
604
604
if setup_module is None and teardown_module is None :
605
605
return
606
606
607
- @fixtures .fixture (
608
- autouse = True ,
609
- scope = "module" ,
610
- # Use a unique name to speed up lookup.
611
- name = f"_xunit_setup_module_fixture_{ self .obj .__name__ } " ,
612
- )
613
607
def xunit_setup_module_fixture (request ) -> Generator [None , None , None ]:
608
+ module = request .module
614
609
if setup_module is not None :
615
- _call_with_optional_argument (setup_module , request . module )
610
+ _call_with_optional_argument (setup_module , module )
616
611
yield
617
612
if teardown_module is not None :
618
- _call_with_optional_argument (teardown_module , request . module )
613
+ _call_with_optional_argument (teardown_module , module )
619
614
620
- self .obj .__pytest_setup_module = xunit_setup_module_fixture
615
+ self .session ._fixturemanager ._register_fixture (
616
+ # Use a unique name to speed up lookup.
617
+ name = f"_xunit_setup_module_fixture_{ self .obj .__name__ } " ,
618
+ func = xunit_setup_module_fixture ,
619
+ nodeid = self .nodeid ,
620
+ scope = "module" ,
621
+ autouse = True ,
622
+ )
621
623
622
- def _inject_setup_function_fixture (self ) -> None :
623
- """Inject a hidden autouse, function scoped fixture into the collected module object
624
+ def _register_setup_function_fixture (self ) -> None :
625
+ """Register an autouse, function- scoped fixture for the collected module object
624
626
that invokes setup_function/teardown_function if either or both are available.
625
627
626
628
Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with
@@ -633,25 +635,27 @@ def _inject_setup_function_fixture(self) -> None:
633
635
if setup_function is None and teardown_function is None :
634
636
return
635
637
636
- @fixtures .fixture (
637
- autouse = True ,
638
- scope = "function" ,
639
- # Use a unique name to speed up lookup.
640
- name = f"_xunit_setup_function_fixture_{ self .obj .__name__ } " ,
641
- )
642
638
def xunit_setup_function_fixture (request ) -> Generator [None , None , None ]:
643
639
if request .instance is not None :
644
640
# in this case we are bound to an instance, so we need to let
645
641
# setup_method handle this
646
642
yield
647
643
return
644
+ function = request .function
648
645
if setup_function is not None :
649
- _call_with_optional_argument (setup_function , request . function )
646
+ _call_with_optional_argument (setup_function , function )
650
647
yield
651
648
if teardown_function is not None :
652
- _call_with_optional_argument (teardown_function , request . function )
649
+ _call_with_optional_argument (teardown_function , function )
653
650
654
- self .obj .__pytest_setup_function = xunit_setup_function_fixture
651
+ self .session ._fixturemanager ._register_fixture (
652
+ # Use a unique name to speed up lookup.
653
+ name = f"_xunit_setup_function_fixture_{ self .obj .__name__ } " ,
654
+ func = xunit_setup_function_fixture ,
655
+ nodeid = self .nodeid ,
656
+ scope = "function" ,
657
+ autouse = True ,
658
+ )
655
659
656
660
657
661
class Package (nodes .Directory ):
@@ -795,15 +799,15 @@ def collect(self) -> Iterable[Union[nodes.Item, nodes.Collector]]:
795
799
)
796
800
return []
797
801
798
- self ._inject_setup_class_fixture ()
799
- self ._inject_setup_method_fixture ()
802
+ self ._register_setup_class_fixture ()
803
+ self ._register_setup_method_fixture ()
800
804
801
805
self .session ._fixturemanager .parsefactories (self .newinstance (), self .nodeid )
802
806
803
807
return super ().collect ()
804
808
805
- def _inject_setup_class_fixture (self ) -> None :
806
- """Inject a hidden autouse, class scoped fixture into the collected class object
809
+ def _register_setup_class_fixture (self ) -> None :
810
+ """Register an autouse, class scoped fixture into the collected class object
807
811
that invokes setup_class/teardown_class if either or both are available.
808
812
809
813
Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with
@@ -814,25 +818,27 @@ def _inject_setup_class_fixture(self) -> None:
814
818
if setup_class is None and teardown_class is None :
815
819
return
816
820
817
- @fixtures .fixture (
818
- autouse = True ,
819
- scope = "class" ,
820
- # Use a unique name to speed up lookup.
821
- name = f"_xunit_setup_class_fixture_{ self .obj .__qualname__ } " ,
822
- )
823
- def xunit_setup_class_fixture (cls ) -> Generator [None , None , None ]:
821
+ def xunit_setup_class_fixture (request ) -> Generator [None , None , None ]:
822
+ cls = request .cls
824
823
if setup_class is not None :
825
824
func = getimfunc (setup_class )
826
- _call_with_optional_argument (func , self . obj )
825
+ _call_with_optional_argument (func , cls )
827
826
yield
828
827
if teardown_class is not None :
829
828
func = getimfunc (teardown_class )
830
- _call_with_optional_argument (func , self . obj )
829
+ _call_with_optional_argument (func , cls )
831
830
832
- self .obj .__pytest_setup_class = xunit_setup_class_fixture
831
+ self .session ._fixturemanager ._register_fixture (
832
+ # Use a unique name to speed up lookup.
833
+ name = f"_xunit_setup_class_fixture_{ self .obj .__qualname__ } " ,
834
+ func = xunit_setup_class_fixture ,
835
+ nodeid = self .nodeid ,
836
+ scope = "class" ,
837
+ autouse = True ,
838
+ )
833
839
834
- def _inject_setup_method_fixture (self ) -> None :
835
- """Inject a hidden autouse, function scoped fixture into the collected class object
840
+ def _register_setup_method_fixture (self ) -> None :
841
+ """Register an autouse, function scoped fixture into the collected class object
836
842
that invokes setup_method/teardown_method if either or both are available.
837
843
838
844
Using a fixture to invoke these methods ensures we play nicely and unsurprisingly with
@@ -845,23 +851,25 @@ def _inject_setup_method_fixture(self) -> None:
845
851
if setup_method is None and teardown_method is None :
846
852
return
847
853
848
- @fixtures .fixture (
849
- autouse = True ,
850
- scope = "function" ,
851
- # Use a unique name to speed up lookup.
852
- name = f"_xunit_setup_method_fixture_{ self .obj .__qualname__ } " ,
853
- )
854
- def xunit_setup_method_fixture (self , request ) -> Generator [None , None , None ]:
854
+ def xunit_setup_method_fixture (request ) -> Generator [None , None , None ]:
855
+ instance = request .instance
855
856
method = request .function
856
857
if setup_method is not None :
857
- func = getattr (self , setup_name )
858
+ func = getattr (instance , setup_name )
858
859
_call_with_optional_argument (func , method )
859
860
yield
860
861
if teardown_method is not None :
861
- func = getattr (self , teardown_name )
862
+ func = getattr (instance , teardown_name )
862
863
_call_with_optional_argument (func , method )
863
864
864
- self .obj .__pytest_setup_method = xunit_setup_method_fixture
865
+ self .session ._fixturemanager ._register_fixture (
866
+ # Use a unique name to speed up lookup.
867
+ name = f"_xunit_setup_method_fixture_{ self .obj .__qualname__ } " ,
868
+ func = xunit_setup_method_fixture ,
869
+ nodeid = self .nodeid ,
870
+ scope = "function" ,
871
+ autouse = True ,
872
+ )
865
873
866
874
867
875
def hasinit (obj : object ) -> bool :
0 commit comments