setupplan.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from typing import Optional
  2. from typing import Union
  3. import pytest
  4. from _pytest.config import Config
  5. from _pytest.config import ExitCode
  6. from _pytest.config.argparsing import Parser
  7. from _pytest.fixtures import FixtureDef
  8. from _pytest.fixtures import SubRequest
  9. def pytest_addoption(parser: Parser) -> None:
  10. group = parser.getgroup("debugconfig")
  11. group.addoption(
  12. "--setupplan",
  13. "--setup-plan",
  14. action="store_true",
  15. help="show what fixtures and tests would be executed but "
  16. "don't execute anything.",
  17. )
  18. @pytest.hookimpl(tryfirst=True)
  19. def pytest_fixture_setup(
  20. fixturedef: FixtureDef[object], request: SubRequest
  21. ) -> Optional[object]:
  22. # Will return a dummy fixture if the setuponly option is provided.
  23. if request.config.option.setupplan:
  24. my_cache_key = fixturedef.cache_key(request)
  25. fixturedef.cached_result = (None, my_cache_key, None)
  26. return fixturedef.cached_result
  27. return None
  28. @pytest.hookimpl(tryfirst=True)
  29. def pytest_cmdline_main(config: Config) -> Optional[Union[int, ExitCode]]:
  30. if config.option.setupplan:
  31. config.option.setuponly = True
  32. config.option.setupshow = True
  33. return None