Coverage for tests / unit / tools / pytest_tool / test_set_options.py: 100%

15 statements  

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

1"""Tests for PytestPlugin.set_options and fix methods.""" 

2 

3from __future__ import annotations 

4 

5from typing import TYPE_CHECKING 

6 

7import pytest 

8from assertpy import assert_that 

9 

10if TYPE_CHECKING: 

11 from lintro.tools.definitions.pytest import PytestPlugin 

12 

13 

14# ============================================================================= 

15# Tests for PytestPlugin set_options with valid values 

16# ============================================================================= 

17 

18 

19@pytest.mark.parametrize( 

20 ("option_name", "option_value"), 

21 [ 

22 ("verbose", True), 

23 ("verbose", False), 

24 ("maxfail", 1), 

25 ("maxfail", 5), 

26 ("tb", "short"), 

27 ("tb", "long"), 

28 ("tb", "auto"), 

29 ("tb", "line"), 

30 ("tb", "native"), 

31 ("junitxml", "report.xml"), 

32 ("json_report", True), 

33 ("workers", "auto"), 

34 ("workers", "4"), 

35 ("coverage_threshold", 80.0), 

36 ("coverage_threshold", 0), 

37 ("coverage_threshold", 100), 

38 ], 

39 ids=[ 

40 "verbose_true", 

41 "verbose_false", 

42 "maxfail_1", 

43 "maxfail_5", 

44 "tb_short", 

45 "tb_long", 

46 "tb_auto", 

47 "tb_line", 

48 "tb_native", 

49 "junitxml", 

50 "json_report", 

51 "workers_auto", 

52 "workers_4", 

53 "coverage_threshold_80", 

54 "coverage_threshold_0", 

55 "coverage_threshold_100", 

56 ], 

57) 

58def test_set_options_valid( 

59 sample_pytest_plugin: PytestPlugin, 

60 option_name: str, 

61 option_value: object, 

62) -> None: 

63 """Set valid options correctly. 

64 

65 Args: 

66 sample_pytest_plugin: PytestPlugin instance for testing. 

67 option_name: Name of the option to set. 

68 option_value: Value to set for the option. 

69 """ 

70 sample_pytest_plugin.set_options(**{option_name: option_value}) 

71 assert_that( 

72 sample_pytest_plugin.pytest_config.get_options_dict().get(option_name), 

73 ).is_equal_to( 

74 option_value, 

75 ) 

76 

77 

78def test_set_options_workers_int_coerces_to_string( 

79 sample_pytest_plugin: PytestPlugin, 

80) -> None: 

81 """Coerce integer workers to string for pytest-xdist.""" 

82 sample_pytest_plugin.set_options(workers=4) 

83 assert_that( 

84 sample_pytest_plugin.pytest_config.get_options_dict().get("workers"), 

85 ).is_equal_to("4") 

86 

87 

88# ============================================================================= 

89# Tests for PytestPlugin set_options with invalid values 

90# ============================================================================= 

91 

92 

93@pytest.mark.parametrize( 

94 ("option_name", "invalid_value", "error_match"), 

95 [ 

96 ("verbose", "yes", "verbose must be a boolean"), 

97 ("maxfail", "5", "maxfail must be a positive integer"), 

98 ("maxfail", -1, "maxfail must be a positive integer"), 

99 ("maxfail", 0, "maxfail must be a positive integer"), 

100 ("tb", "invalid", "tb must be one of"), 

101 ("junitxml", 123, "junitxml must be a string"), 

102 ("json_report", "yes", "json_report must be a boolean"), 

103 ("workers", ["4"], "workers must be a string"), 

104 ("coverage_threshold", "80", "coverage_threshold must be a number"), 

105 ("coverage_threshold", -1, "coverage_threshold must be between 0 and 100"), 

106 ("coverage_threshold", 101, "coverage_threshold must be between 0 and 100"), 

107 ("timeout", "30", "timeout must be a positive integer"), 

108 ("timeout", -1, "timeout must be a positive integer"), 

109 ("reruns", "3", "reruns must be a non-negative integer"), 

110 ("reruns", -1, "reruns must be a non-negative integer"), 

111 ], 

112 ids=[ 

113 "invalid_verbose_type", 

114 "invalid_maxfail_string", 

115 "invalid_maxfail_negative", 

116 "invalid_maxfail_zero", 

117 "invalid_tb_value", 

118 "invalid_junitxml_type", 

119 "invalid_json_report_type", 

120 "invalid_workers_type", 

121 "invalid_coverage_threshold_type", 

122 "invalid_coverage_threshold_negative", 

123 "invalid_coverage_threshold_over_100", 

124 "invalid_timeout_string", 

125 "invalid_timeout_negative", 

126 "invalid_reruns_string", 

127 "invalid_reruns_negative", 

128 ], 

129) 

130def test_set_options_invalid_type( 

131 sample_pytest_plugin: PytestPlugin, 

132 option_name: str, 

133 invalid_value: object, 

134 error_match: str, 

135) -> None: 

136 """Raise ValueError for invalid option types. 

137 

138 Args: 

139 sample_pytest_plugin: PytestPlugin instance for testing. 

140 option_name: Name of the option to set. 

141 invalid_value: Invalid value that should cause an error. 

142 error_match: Expected error message pattern. 

143 """ 

144 with pytest.raises(ValueError, match=error_match): 

145 sample_pytest_plugin.set_options(**{option_name: invalid_value})