Coverage for human_requests / pytest_plugin / _config.py: 81%
43 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-07 17:38 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-07 17:38 +0000
1from __future__ import annotations
3import importlib
4import inspect
5from typing import Any
7import pytest
9from ._constants import AUTOTEST_INI_KEY, AUTOTEST_TYPECHECK_INI_KEY, VALID_TYPECHECK_MODES
12def register_ini_options(parser: pytest.Parser) -> None:
13 parser.addini(
14 AUTOTEST_INI_KEY,
15 default="",
16 help="Dotted import path to the root API class, e.g. package_name.StartClass",
17 )
18 parser.addini(
19 AUTOTEST_TYPECHECK_INI_KEY,
20 default="off",
21 help="Autotest params type checking mode: off, warn, strict.",
22 )
25def get_start_class_path(config: pytest.Config) -> str:
26 return str(config.getini(AUTOTEST_INI_KEY)).strip()
29def get_typecheck_mode(config: pytest.Config) -> str:
30 raw = str(config.getini(AUTOTEST_TYPECHECK_INI_KEY)).strip().lower()
31 if not raw:
32 return "off"
33 if raw in VALID_TYPECHECK_MODES:
34 return raw
36 expected = ", ".join(sorted(VALID_TYPECHECK_MODES))
37 raise pytest.UsageError(
38 f"Invalid {AUTOTEST_TYPECHECK_INI_KEY} value {raw!r}. Expected one of: {expected}."
39 )
42def resolve_runtime_dependencies(request: pytest.FixtureRequest) -> tuple[object, Any]:
43 start_class_path = get_start_class_path(request.config)
44 if not start_class_path:
45 raise pytest.UsageError(
46 f"{AUTOTEST_INI_KEY} must be configured when autotest plugin is enabled."
47 )
49 start_class = import_start_class(start_class_path)
51 api = request.getfixturevalue("api")
52 if not isinstance(api, start_class):
53 expected = f"{start_class.__module__}.{start_class.__qualname__}"
54 actual = f"{type(api).__module__}.{type(api).__qualname__}"
55 raise TypeError(f"`api` fixture must return an instance of {expected}. " f"Got {actual}.")
57 schemashot = request.getfixturevalue("schemashot")
58 return api, schemashot
61def import_start_class(dotted_path: str) -> type[Any]:
62 module_name, separator, class_name = dotted_path.rpartition(".")
63 if not separator or not module_name or not class_name:
64 raise pytest.UsageError(
65 f"Invalid {AUTOTEST_INI_KEY} value {dotted_path!r}. Expected format: module.StartClass"
66 )
68 try:
69 module = importlib.import_module(module_name)
70 except Exception as error: # pragma: no cover - importlib already provides full details
71 raise pytest.UsageError(
72 f"Cannot import module {module_name!r} from {AUTOTEST_INI_KEY}={dotted_path!r}."
73 ) from error
75 if not hasattr(module, class_name):
76 raise pytest.UsageError(
77 f"Class {class_name!r} was not found in module {module_name!r} "
78 f"from {AUTOTEST_INI_KEY}={dotted_path!r}."
79 )
81 start_class = getattr(module, class_name)
82 if not inspect.isclass(start_class):
83 raise pytest.UsageError(f"{AUTOTEST_INI_KEY}={dotted_path!r} must point to a class.")
85 return start_class