-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_hypothesis.py
49 lines (36 loc) · 1.32 KB
/
gen_hypothesis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import pkgutil
import subprocess
from pathlib import Path
from typing import Any
import cli_tool_audit
def get_submodules(module: Any) -> list[str]:
"""
Discovers all submodules in the given module.
Args:
module: The module to discover submodules in.
Returns:
A list of submodule names.
"""
package_path = module.__path__
return [name for _, name, _ in pkgutil.iter_modules(package_path)]
def generate_hypothesis_tests(module: Any) -> None:
"""
Generates and executes Hypothesis test commands for each submodule in the given module.
Args:
module: The module to generate tests for.
"""
submodules = get_submodules(module)
base_command = ['hypothesis', 'write', '--style', 'pytest', '--annotate']
output_dir = Path('tests/test_hypothesis/')
output_dir.mkdir(parents=True, exist_ok=True)
for submodule in submodules:
module_name = f'{module.__name__}.{submodule}'
test_file_name = f'test_{submodule}.py'
output_path = output_dir / test_file_name
command = base_command + [module_name]
with open(output_path, 'w') as file:
print(command)
subprocess.run(command, stdout=file, text=True, check=True)
# Example usage:
if __name__ == '__main__':
generate_hypothesis_tests(cli_tool_audit)