Skip to main content

CLI to generate and update Python unit tests automatically using coverage and AI

Project description

AIUnitTest

PyPI version Python versions License: MIT Code style: black Imports: isort Linter: flake8 Static typing: mypy Testing: pytest Coverage CI/CD GitHub last commit GitHub repo size GitHub issues

AIUnitTest is a command-line tool that reads your pyproject.toml and test coverage data (.coverage) to generate and update missing Python unit tests using AI.

How it Works

  1. Coverage Analysis: The tool uses coverage.py to identify lines of code that are not covered by your existing test suite.
  2. Source Code Chunking: It breaks down the source code into logical chunks (functions and classes).
  3. AI-Powered Test Generation: For each chunk with uncovered lines, it sends the source code and the uncovered line numbers to an AI model (like OpenAI's GPT) to generate new test cases.
  4. Test File Updates: The newly generated tests are appended to the corresponding test file.

Features

  • Coverage Analysis: Uses the Coverage.py API to identify untested lines.
  • AI-Powered Test Generation: Calls OpenAI GPT to create or enhance test cases.
  • Config-Driven: Automatically picks up coverage.run.source and pytest.ini_options.testpaths from pyproject.toml.
  • Auto Mode: The --auto flag sets source and tests directories without manual arguments.
  • Async & Parallel: Speeds up OpenAI requests for large codebases.

Installation

There are two ways to install AIUnitTest:

From PyPI

You can install the latest stable version from PyPI:

pip install AIUnitTest

Optional Dependencies

AIUnitTest supports optional features that can be installed as needed:

Basic Installation (OpenAI only)

pip install AIUnitTest

With HuggingFace Support

pip install AIUnitTest[huggingface]

With FAISS Indexing Support

pip install AIUnitTest[faiss]

All Optional Features

pip install AIUnitTest[all]

Development Dependencies

pip install AIUnitTest[dev]

You can also combine multiple extras:

pip install AIUnitTest[huggingface,faiss]

From GitHub (for the latest development version)

  1. Clone the repository:

    git clone https://github.com/ofido/AIUnitTest.git
    cd AIUnitTest
    
  2. Install the project in editable mode:

    pip install -e .
    # Or with optional dependencies:
    pip install -e .[all]
    

Usage

Automatic Mode

The easiest way to run the tool is in automatic mode. It will automatically discover your source and test folders from your pyproject.toml file.

ai-unit-test --auto

Manual Mode

You can also specify the source and test folders manually:

ai-unit-test --folders src --tests-folder tests

Generating a Test for a Specific Function

You can also generate a test for a single function:

ai-unit-test func my_module/my_file.py my_function

Indexing and Searching Tests

You can index your tests for semantic search:

ai-unit-test index

And then search for tests related to a specific query:

ai-unit-test search "my search query"

Command-Line Options

  • --folders: The source code folders to analyze.
  • --tests-folder: The folder where the tests are located.
  • --coverage-file: The path to the .coverage file.
  • --auto: Try to discover folders/tests from pyproject.toml.

Configuration

AIUnitTest uses the standard pyproject.toml file for configuration. Here are the relevant sections:

  • [tool.coverage.run]:
    • source: A list of source code folders.
  • [tool.pytest.ini_options]:
    • testpaths: A list of test folders.

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue.

License

This project is licensed under the MIT License see the LICENSE file for details.

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

aiunittest-0.0.7.tar.gz (102.8 kB view details)

Uploaded Source

Built Distribution

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

aiunittest-0.0.7-py3-none-any.whl (56.2 kB view details)

Uploaded Python 3

File details

Details for the file aiunittest-0.0.7.tar.gz.

File metadata

  • Download URL: aiunittest-0.0.7.tar.gz
  • Upload date:
  • Size: 102.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for aiunittest-0.0.7.tar.gz
Algorithm Hash digest
SHA256 3a4a7baa5a3b6c899f2cecab462e3506dc302c91d2b95682c8b114e997bbf52e
MD5 bbf1bbc0026ec0bf24828fb6a1f63e2d
BLAKE2b-256 a5eac7e0069542fdace5bc36604810f89585ab8d29b0fe262a5e12c82a7993c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiunittest-0.0.7.tar.gz:

Publisher: release.yml on Ofido/AIUnitTest

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file aiunittest-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: aiunittest-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 56.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for aiunittest-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 37ab93a2125675ce87b6ef4b38906ffc573ab5cf0abaf804465f5ea5f9aa2ae2
MD5 d0f2ea1f67b7934ce12841309610bc0b
BLAKE2b-256 f02af6bb066d44eabd65991d7dbadd6367bcfefa948ccea90b45167eb75ff9af

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiunittest-0.0.7-py3-none-any.whl:

Publisher: release.yml on Ofido/AIUnitTest

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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