scripts.test-gen
Reference › scripts › test-gen
scripts/test-gen.py is the global test generation tool: it prints test stubs for Python source, or reports test coverage, from any directory where orchestrata-tools is installed. A human runs it. It uses SourceASTParser and ASTIndex from orchestrata_tools.parser, TestMatcher and CoverageAnalyzer from orchestrata_tools.test_analyzer, and TestStubGenerator from orchestrata_tools.test_generator. For the repository's own generator see generate_pytest.
It takes no subcommands. find_project_root locates the project, and the script parses its src directory (or the root if there is none). With --coverage it matches the parsed tests directory against the source and prints total methods, tested methods and the method coverage percentage. Otherwise it prints stubs from generate_class_stubs for the first 15 classes, each cut to 500 characters, and a count of the remaining classes. It prints to standard output only.
At a glance
| Source | scripts/test-gen.py |
| Lines of code | 67 |
| Classes | 0 |
| Functions | 2 |
| Imports (internal) | orchestrata_tools.parser, orchestrata_tools.test_analyzer, orchestrata_tools.test_generator |
| Imported by | none |
| Tests that import it | none |
Class diagram
No classes; see Functions.
Dependency diagram
API
Functions
| Function | Signature | Summary |
|---|---|---|
find_project_root |
find_project_root() -> Path | None |
|
main |
main() -> None |
Source
Permalink: https://gitlab.com/MSamy/orchestrata-ultimate-architecture/-/blob/a4cbc2bdf/scripts/test-gen.py
#!/usr/bin/env python3
"""Global test generation tool — generate test stubs from Python source.
Usage:
test-gen src/mymodule.py # Generate tests for a single file
test-gen --dir src/ # Generate tests for a directory
test-gen --coverage src/ tests/ # Analyze test coverage
"""
import sys
from pathlib import Path
def find_project_root() -> Path | None:
current = Path.cwd().resolve()
for parent in [current, *current.parents]:
if (parent / "pyproject.toml").exists():
return parent
return None
def main() -> None:
root = find_project_root()
if root is None:
root = Path.cwd()
from orchestrata_tools.parser import ASTIndex, SourceASTParser
from orchestrata_tools.test_analyzer import CoverageAnalyzer, TestMatcher
from orchestrata_tools.test_generator import TestStubGenerator
parser = SourceASTParser()
source_dir = root / "src"
test_dir = root / "tests"
codebase = parser.parse_directory(
str(source_dir) if source_dir.exists() else str(root)
)
index = ASTIndex(codebase)
if "--coverage" in sys.argv:
test_codebase = (
parser.parse_directory(str(test_dir)) if test_dir.exists() else codebase
)
matcher = TestMatcher(test_codebase)
analyzer = CoverageAnalyzer(matcher, index)
report = analyzer.analyze()
print("=== TEST COVERAGE ===")
print(f"Total methods: {report.total_methods}")
print(f"Tested methods: {report.tested_methods}")
print(f"Coverage: {report.method_coverage_pct}%")
return
print(
f"=== TEST GENERATION: {codebase.total_classes} classes, {codebase.total_loc} LOC ==="
)
generator = TestStubGenerator()
classes = codebase.all_classes
for cls in classes[:15]:
print(f"\n# --- {cls.file_path} ---")
print(generator.generate_class_stubs(cls)[:500])
if len(classes) > 15:
print(f"\n... and {len(classes) - 15} more classes")
if __name__ == "__main__":
main()