Skip to main content

Coverage-guided test generator for Python

Project description

License CI PyPI Tests Coverage Bandit Black Ruff Docs

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 pytest files 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 run ghtest in a safe environment.

Installation

pip install ghtest

Usage

Run ghtest on your source directory:

ghtest src/my_project

This will:

  1. Scan your code for functions.
  2. Generate initial tests.
  3. Run them to capture behavior (recording cassettes).
  4. Analyze coverage to find missed branches.
  5. Generate targeted tests to fill coverage gaps.
  6. 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

  1. Static Analysis: ghtest parses your code to understand function signatures, types, and dependencies.
  2. Parameter Suggestion: It suggests input values based on types and common patterns.
  3. 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.
  4. 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.
  5. Code Generation: Finally, it writes out standard Python test files that assert the captured behavior matches the replay.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ghtest-0.1.6.tar.gz (375.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ghtest-0.1.6-py3-none-any.whl (48.3 kB view details)

Uploaded Python 3

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

Hashes for ghtest-0.1.6.tar.gz
Algorithm Hash digest
SHA256 e92006dd32912e55c0743e03d93056ebbb2057c42321c9a2b7e87f01d3aab418
MD5 d230b915dc1394a1049adfc6c3485ef5
BLAKE2b-256 763b2f1184fb84f31cd0b09edf9d4a16a87fe1d38cbdb1d5ff42b42566999fef

See more details on using hashes here.

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

Hashes for ghtest-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 60d4512158d9eaba0026ecf9b4eda6a6310121424026a5ebf4ae61a1a5caec34
MD5 7246a7e493d644e2c32e41f11a562043
BLAKE2b-256 883366220681f8f0c31c1f989f1ff8b5b3033fe79135868b285185bacc048001

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page