Coverage for src/rhiza_task/tasks/extras.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-11-30 09:28 +0000

1"""The optional testing extras: test.mk, as tasks. 

2 

3Three gates no ``all`` depends on -- ``book`` is the one aggregate that names them, for 

4their reports -- each needing its own tool and folder convention. They stay separate from 

5the language layer for the reason test.mk gives: a project should be able to take the Python 

6gate set without also declaring an opinion on benchmarks, stress runs or property-based 

7testing. 

8 

9Each body is one vector plus, for ``hypothesis-test``, a single exit code: pytest's "no 

10tests collected", which is a skip rather than a failure for a project that has none. 

11""" 

12 

13from __future__ import annotations 

14 

15from ..config import Config 

16from ..spec import Failed, Guard, task 

17from ..uv import uv_run 

18 

19PYTEST_NO_TESTS_COLLECTED = 5 

20"""pytest's "no tests collected". 

21 

22For ``hypothesis-test`` this is a skip, not a failure: a project with no property-based 

23tests is a valid project, and the marker expression legitimately matches nothing. 

24""" 

25 

26 

27@task( 

28 "benchmark", 

29 "run the performance benchmarks", 

30 section="Testing extras", 

31 layer="python", 

32 needs=("install",), 

33 guards=(Guard("tests_folder", glob="benchmarks/*.py", reason="no benchmarks folder"),), 

34) 

35def benchmark(cfg: Config) -> None: 

36 """Run pytest-benchmark over ``tests/benchmarks``, writing a histogram and JSON. 

37 

38 The two pins are kept exact: benchmark results are only comparable across runs of the 

39 same tool version. ``pytest`` is deliberately unpinned, so the benchmark pin must move 

40 when pytest does -- 5.2.3 read ``node._nodeid``, which pytest 9.2 removed. 

41 

42 Args: 

43 cfg: The resolved config. 

44 """ 

45 (cfg.root / "_tests" / "benchmarks").mkdir(parents=True, exist_ok=True) 

46 uv_run( 

47 "pytest", 

48 f"{cfg.tests_folder}/benchmarks/", 

49 "--benchmark-only", 

50 "--benchmark-histogram=_tests/benchmarks/histogram", 

51 "--benchmark-json=_tests/benchmarks/results.json", 

52 cwd=cfg.root, 

53 withs=("pytest", "pytest-benchmark==5.3.1", "pygal==3.1.0"), 

54 ) 

55 

56 

57@task( 

58 "hypothesis-test", 

59 "run the property-based tests", 

60 section="Testing extras", 

61 layer="python", 

62 needs=("install",), 

63 guards=(Guard("tests_folder", glob="test_*.py", reason="no test files found"),), 

64) 

65def hypothesis_test(cfg: Config) -> None: 

66 """Run the Hypothesis-marked tests with statistics and a fixed seed. 

67 

68 Args: 

69 cfg: The resolved config. 

70 

71 Raises: 

72 Failed: When a property test fails. 

73 """ 

74 (cfg.root / "_tests" / "hypothesis").mkdir(parents=True, exist_ok=True) 

75 code = uv_run( 

76 "pytest", 

77 f"--ignore={cfg.tests_folder}/benchmarks", 

78 "-v", 

79 "--hypothesis-show-statistics", 

80 "--hypothesis-seed=0", 

81 "-m", 

82 "hypothesis or property", 

83 "--tb=short", 

84 "--html=_tests/hypothesis/report.html", 

85 cwd=cfg.root, 

86 withs=("pytest", "hypothesis", "pytest-html"), 

87 env={"PYTEST_HTML_TITLE": "Hypothesis tests"}, 

88 check=False, 

89 ) 

90 if code == PYTEST_NO_TESTS_COLLECTED: 

91 print("[INFO] no hypothesis/property tests collected") 

92 return 

93 if code: 

94 raise Failed(code, "property tests failed") 

95 

96 

97@task( 

98 "stress", 

99 "run the stress and load tests", 

100 section="Testing extras", 

101 layer="python", 

102 needs=("install",), 

103 guards=(Guard("tests_folder", glob="stress/*.py", reason="no stress folder"),), 

104) 

105def stress(cfg: Config) -> None: 

106 """Run the stress-marked tests. 

107 

108 Args: 

109 cfg: The resolved config. 

110 """ 

111 (cfg.root / "_tests" / "stress").mkdir(parents=True, exist_ok=True) 

112 uv_run( 

113 "pytest", 

114 "-v", 

115 "-m", 

116 "stress", 

117 "--tb=short", 

118 "--html=_tests/stress/report.html", 

119 cwd=cfg.root, 

120 withs=("pytest", "pytest-html"), 

121 )