MCPcopy Create free account
hub / github.com/pytest-dev/pytest / _inject_setup_module_fixture

Method _inject_setup_module_fixture

src/_pytest/python.py:518–559  ·  view source on GitHub ↗

Inject a hidden autouse, module scoped fixture into the collected module object that invokes setUpModule/tearDownModule if either or both are available. Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with other fixtures (#517).

(self)

Source from the content-addressed store, hash-verified

516 return super().collect()
517
518 def _inject_setup_module_fixture(self) -> None:
519 """Inject a hidden autouse, module scoped fixture into the collected module object
520 that invokes setUpModule/tearDownModule if either or both are available.
521
522 Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with
523 other fixtures (#517).
524 """
525 has_nose = self.config.pluginmanager.has_plugin("nose")
526 setup_module = _get_first_non_fixture_func(
527 self.obj, ("setUpModule", "setup_module")
528 )
529 if setup_module is None and has_nose:
530 # The name "setup" is too common - only treat as fixture if callable.
531 setup_module = _get_first_non_fixture_func(self.obj, ("setup",))
532 if not callable(setup_module):
533 setup_module = None
534 teardown_module = _get_first_non_fixture_func(
535 self.obj, ("tearDownModule", "teardown_module")
536 )
537 if teardown_module is None and has_nose:
538 teardown_module = _get_first_non_fixture_func(self.obj, ("teardown",))
539 # Same as "setup" above - only treat as fixture if callable.
540 if not callable(teardown_module):
541 teardown_module = None
542
543 if setup_module is None and teardown_module is None:
544 return
545
546 @fixtures.fixture(
547 autouse=True,
548 scope="module",
549 # Use a unique name to speed up lookup.
550 name=f"_xunit_setup_module_fixture_{self.obj.__name__}",
551 )
552 def xunit_setup_module_fixture(request) -> Generator[None, None, None]:
553 if setup_module is not None:
554 _call_with_optional_argument(setup_module, request.module)
555 yield
556 if teardown_module is not None:
557 _call_with_optional_argument(teardown_module, request.module)
558
559 self.obj.__pytest_setup_module = xunit_setup_module_fixture
560
561 def _inject_setup_function_fixture(self) -> None:
562 """Inject a hidden autouse, function scoped fixture into the collected module object

Callers 1

collectMethod · 0.95

Calls 1

Tested by

no test coverage detected