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