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

1from __future__ import annotations 

2 

3import importlib 

4import inspect 

5from typing import Any 

6 

7import pytest 

8 

9from ._constants import AUTOTEST_INI_KEY, AUTOTEST_TYPECHECK_INI_KEY, VALID_TYPECHECK_MODES 

10 

11 

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 ) 

23 

24 

25def get_start_class_path(config: pytest.Config) -> str: 

26 return str(config.getini(AUTOTEST_INI_KEY)).strip() 

27 

28 

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 

35 

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 ) 

40 

41 

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 ) 

48 

49 start_class = import_start_class(start_class_path) 

50 

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}.") 

56 

57 schemashot = request.getfixturevalue("schemashot") 

58 return api, schemashot 

59 

60 

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 ) 

67 

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 

74 

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 ) 

80 

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.") 

84 

85 return start_class