Coverage for lintro / enums / pytest_enums.py: 100%

47 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-04-03 18:53 +0000

1"""Pytest-related enum definitions. 

2 

3This module defines enums for pytest special modes, output formats, and test statuses. 

4""" 

5 

6from __future__ import annotations 

7 

8from enum import StrEnum, auto 

9 

10 

11class TestStatus(StrEnum): 

12 """Supported test status values. 

13 

14 Values are lower-case string identifiers (auto-generated from member names). 

15 """ 

16 

17 PASSED = auto() 

18 FAILED = auto() 

19 ERROR = auto() 

20 SKIPPED = auto() 

21 UNKNOWN = auto() 

22 

23 

24class PytestSpecialMode(StrEnum): 

25 """Supported special modes for pytest execution. 

26 

27 Values are lower-case string identifiers to align with pytest command-line options. 

28 """ 

29 

30 LIST_PLUGINS = auto() 

31 CHECK_PLUGINS = auto() 

32 COLLECT_ONLY = auto() 

33 LIST_FIXTURES = auto() 

34 FIXTURE_INFO = auto() 

35 LIST_MARKERS = auto() 

36 PARAMETRIZE_HELP = auto() 

37 

38 

39class PytestOutputFormat(StrEnum): 

40 """Supported output formats for pytest results. 

41 

42 Values are lower-case string identifiers to align with pytest options. 

43 """ 

44 

45 JSON = auto() 

46 JUNIT = auto() 

47 TEXT = auto() 

48 

49 

50class PytestParallelPreset(StrEnum): 

51 """Supported parallel execution presets for pytest-xdist. 

52 

53 Values are lower-case string identifiers for preset names. 

54 """ 

55 

56 AUTO = auto() 

57 SMALL = auto() 

58 MEDIUM = auto() 

59 LARGE = auto() 

60 

61 

62def normalize_pytest_special_mode(value: str | PytestSpecialMode) -> PytestSpecialMode: 

63 """Normalize a raw value to a PytestSpecialMode enum. 

64 

65 Args: 

66 value: str or PytestSpecialMode to normalize. 

67 

68 Returns: 

69 PytestSpecialMode: Normalized enum value. 

70 

71 Raises: 

72 ValueError: If value is not a valid special mode. 

73 """ 

74 if isinstance(value, PytestSpecialMode): 

75 return value 

76 try: 

77 return PytestSpecialMode[value.upper()] 

78 except KeyError as err: 

79 raise ValueError( 

80 f"Unknown pytest special mode: {value!r}. " 

81 f"Supported modes: {list(PytestSpecialMode)}", 

82 ) from err 

83 

84 

85def normalize_pytest_output_format( 

86 value: str | PytestOutputFormat, 

87) -> PytestOutputFormat: 

88 """Normalize a raw value to a PytestOutputFormat enum. 

89 

90 Args: 

91 value: str or PytestOutputFormat to normalize. 

92 

93 Returns: 

94 PytestOutputFormat: Normalized enum value. 

95 

96 Raises: 

97 ValueError: If value is not a valid output format. 

98 """ 

99 if isinstance(value, PytestOutputFormat): 

100 return value 

101 try: 

102 return PytestOutputFormat[value.upper()] 

103 except KeyError as err: 

104 raise ValueError( 

105 f"Unknown pytest output format: {value!r}. " 

106 f"Supported formats: {list(PytestOutputFormat)}", 

107 ) from err 

108 

109 

110def normalize_test_status(value: str | TestStatus) -> TestStatus: 

111 """Normalize a raw value to a TestStatus enum. 

112 

113 Args: 

114 value: str or TestStatus to normalize. 

115 

116 Returns: 

117 TestStatus: Normalized enum value. 

118 

119 Raises: 

120 ValueError: If value is not a valid test status. 

121 """ 

122 if isinstance(value, TestStatus): 

123 return value 

124 try: 

125 return TestStatus[value.upper()] 

126 except KeyError as err: 

127 supported = f"Supported statuses: {list(TestStatus)}" 

128 raise ValueError( 

129 f"Unknown test status: {value!r}. {supported}", 

130 ) from err