1 from typing import Optional

2 from typing import Union

3

4 import pytest

5 from _pytest.config import Config

6 from _pytest.config import ExitCode

7 from _pytest.config.argparsing import Parser

8 from _pytest.fixtures import FixtureDef

9 from _pytest.fixtures import SubRequest

10

11

12 def pytest_addoption(parser: Parser) -> None:

13 group = parser.getgroup("debugconfig")

14 group.addoption(

15 "--setupplan",

16 "--setup-plan",

17 action="store_true",

18 help="Show what fixtures and tests would be executed but "

19 "don't execute anything",

20 )

21

22

23@pytest.hookimpl(tryfirst=True)

24 def pytest_fixture_setup(

25 fixturedef: FixtureDef[object], request: SubRequest

26) -> Optional[object]:

27 # Will return a dummy fixture if the setuponly option is provided.

28 if request.config.option.setupplan:

29 my_cache_key = fixturedef.cache_key(request)

30 fixturedef.cached_result = (None, my_cache_key, None)

31 return fixturedef.cached_result

32 return None

33

34

35@pytest.hookimpl(tryfirst=True)

36 def pytest_cmdline_main(config: Config) -> Optional[Union[int, ExitCode]]:

37 if config.option.setupplan:

38 config.option.setuponly = True

39 config.option.setupshow = True

40 return None