Coverage-guided test generator for Python
Project description
ghtest
Coverage-Guided Test Generator for Python
ghtest is a test generation tool that automatically creates regression tests for your Python code. It combines static analysis with coverage-guided feedback to generate (hopefully) high-quality tests that capture the current behavior of your application.
Key Features
- Coverage-Guided Generation: Iteratively generates tests to target missed branches and edge cases.
- VCR Integration: Automatically records and replays external API interactions (like GitHub API calls) using
vcrpy, making tests deterministic and offline-capable. - Scenario Support: Detects CRUD patterns and generates stateful scenario tests (e.g., create -> read -> delete).
- Smart Parameter Suggestion: Analyzes function signatures and type hints to suggest realistic test inputs.
- Portable Tests: Generates standalone
pytestfiles with relative paths, ready to be committed to your repository. - Safe Execution: Includes an interactive mode to prevent accidental execution of destructive functions (like
delete_repo) during test generation. Nevertheless, it is stringly recommended to runghtestin a safe environment.
Installation
pip install ghtest
Usage
Run ghtest on your source directory:
ghtest src/my_project
This will:
- Scan your code for functions.
- Generate initial tests.
- Run them to capture behavior (recording cassettes).
- Analyze coverage to find missed branches.
- Generate targeted tests to fill coverage gaps.
- Output ready-to-run test files in
tests/.
Options
| Option | Description |
|---|---|
--test-dir DIR |
Directory to output generated tests (default: tests) |
--cassette-dir DIR |
Directory to store VCR cassettes (default: tests/cassettes) |
--unsafe |
Run potentially destructive functions without asking for permission |
--interactive, -i |
Enable interactive mode (ask before running destructive tests) |
--verbose, -v |
Increase verbosity level |
--no-cleanup |
Do not clean up existing tests/cassettes before running |
--history |
Use parameter history for suggestions |
--version |
Show version number |
Example
Generate tests for the current directory, asking for permission before running anything risky:
ghtest . --interactive --verbose
How It Works
- Static Analysis:
ghtestparses your code to understand function signatures, types, and dependencies. - Parameter Suggestion: It suggests input values based on types and common patterns.
- Execution & Capture: It runs the functions with the suggested inputs.
- Return values and exceptions are captured.
- File I/O is tracked.
- Network requests are recorded into VCR cassettes.
- Coverage Feedback: It measures code coverage and identifies unexecuted branches (e.g.,
if x > 5:). It then solves for inputs that would trigger those branches and generates new tests. - Code Generation: Finally, it writes out standard Python test files that assert the captured behavior matches the replay.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ghtest-0.1.6.tar.gz.
File metadata
- Download URL: ghtest-0.1.6.tar.gz
- Upload date:
- Size: 375.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e92006dd32912e55c0743e03d93056ebbb2057c42321c9a2b7e87f01d3aab418
|
|
| MD5 |
d230b915dc1394a1049adfc6c3485ef5
|
|
| BLAKE2b-256 |
763b2f1184fb84f31cd0b09edf9d4a16a87fe1d38cbdb1d5ff42b42566999fef
|
File details
Details for the file ghtest-0.1.6-py3-none-any.whl.
File metadata
- Download URL: ghtest-0.1.6-py3-none-any.whl
- Upload date:
- Size: 48.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60d4512158d9eaba0026ecf9b4eda6a6310121424026a5ebf4ae61a1a5caec34
|
|
| MD5 |
7246a7e493d644e2c32e41f11a562043
|
|
| BLAKE2b-256 |
883366220681f8f0c31c1f989f1ff8b5b3033fe79135868b285185bacc048001
|