Register an 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 these methods ensures we play nicely and unsurprisingly with other fixtures (#517).
(self)
| 817 | ) |
| 818 | |
| 819 | def _register_setup_method_fixture(self) -> None: |
| 820 | """Register an autouse, function scoped fixture into the collected class object |
| 821 | that invokes setup_method/teardown_method if either or both are available. |
| 822 | |
| 823 | Using a fixture to invoke these methods ensures we play nicely and unsurprisingly with |
| 824 | other fixtures (#517). |
| 825 | """ |
| 826 | setup_name = "setup_method" |
| 827 | setup_method = _get_first_non_fixture_func(self.obj, (setup_name,)) |
| 828 | teardown_name = "teardown_method" |
| 829 | teardown_method = _get_first_non_fixture_func(self.obj, (teardown_name,)) |
| 830 | if setup_method is None and teardown_method is None: |
| 831 | return |
| 832 | |
| 833 | def xunit_setup_method_fixture(request) -> Generator[None]: |
| 834 | instance = request.instance |
| 835 | method = request.function |
| 836 | if setup_method is not None: |
| 837 | func = getattr(instance, setup_name) |
| 838 | _call_with_optional_argument(func, method) |
| 839 | yield |
| 840 | if teardown_method is not None: |
| 841 | func = getattr(instance, teardown_name) |
| 842 | _call_with_optional_argument(func, method) |
| 843 | |
| 844 | fixtures.register_fixture( |
| 845 | # Use a unique name to speed up lookup. |
| 846 | name=f"_xunit_setup_method_fixture_{self.obj.__qualname__}", |
| 847 | func=xunit_setup_method_fixture, |
| 848 | node=self, |
| 849 | scope="function", |
| 850 | autouse=True, |
| 851 | ) |
| 852 | |
| 853 | |
| 854 | def hasinit(obj: object) -> bool: |
no test coverage detected