Inject a hidden autouse, function scoped fixture into the collected class object that invokes setup_method/teardown_method if either or both are available. Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with other fixtures (#517).
(self)
| 851 | self.obj.__pytest_setup_class = xunit_setup_class_fixture |
| 852 | |
| 853 | def _inject_setup_method_fixture(self) -> None: |
| 854 | """Inject a hidden autouse, function scoped fixture into the collected class object |
| 855 | that invokes setup_method/teardown_method if either or both are available. |
| 856 | |
| 857 | Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with |
| 858 | other fixtures (#517). |
| 859 | """ |
| 860 | has_nose = self.config.pluginmanager.has_plugin("nose") |
| 861 | setup_name = "setup_method" |
| 862 | setup_method = _get_first_non_fixture_func(self.obj, (setup_name,)) |
| 863 | if setup_method is None and has_nose: |
| 864 | setup_name = "setup" |
| 865 | setup_method = _get_first_non_fixture_func(self.obj, (setup_name,)) |
| 866 | teardown_name = "teardown_method" |
| 867 | teardown_method = getattr(self.obj, teardown_name, None) |
| 868 | if teardown_method is None and has_nose: |
| 869 | teardown_name = "teardown" |
| 870 | teardown_method = getattr(self.obj, teardown_name, None) |
| 871 | if setup_method is None and teardown_method is None: |
| 872 | return |
| 873 | |
| 874 | @fixtures.fixture( |
| 875 | autouse=True, |
| 876 | scope="function", |
| 877 | # Use a unique name to speed up lookup. |
| 878 | name=f"_xunit_setup_method_fixture_{self.obj.__qualname__}", |
| 879 | ) |
| 880 | def xunit_setup_method_fixture(self, request) -> Generator[None, None, None]: |
| 881 | method = request.function |
| 882 | if setup_method is not None: |
| 883 | func = getattr(self, setup_name) |
| 884 | _call_with_optional_argument(func, method) |
| 885 | yield |
| 886 | if teardown_method is not None: |
| 887 | func = getattr(self, teardown_name) |
| 888 | _call_with_optional_argument(func, method) |
| 889 | |
| 890 | self.obj.__pytest_setup_method = xunit_setup_method_fixture |
| 891 | |
| 892 | |
| 893 | class InstanceDummy: |
no test coverage detected