Skip to main content

Automatically generate pytest test cases for your Python app using Claude AI.

Project description

TestCases Writer AI Tool

Automatically generate comprehensive pytest test cases for your Python application using Claude AI.

Features

  • Scans your Python project and sends code to Claude AI to generate high-quality pytest test cases.
  • Smart caching: Only makes API calls when your code has actually changed, saving costs and time.
  • Writes the generated tests to a tests/ directory in your project.
  • Ensures test files are import-safe by patching sys.path.
  • Supports file-based storage patching for isolated tests (see advanced usage).

Requirements

  • Python 3.7+
  • Anthropic Claude API key
  • Internet connection (for API calls)
  • Packages in requirements.txt (install with pip install -r requirements.txt)

Installation

  1. Clone this repository (or copy the tool files):

    git clone <your-repo-url>
    cd TestCases_Writer_AI_Tool
    
  2. Install dependencies:

    pip install -r requirements.txt
    
  3. Set your Anthropic API key:

    export ANTHROPIC_API_KEY=sk-ant-...
    

Usage

  1. Run the tool on your Python app:

    generate-test-cases /path/to/your/python/app
    
    • Replace /path/to/your/python/app with the path to the root of your Python project.
    • The tool will scan all .py files, send them to Claude, and write generated tests to a tests/ directory inside your app.
  2. Run the generated tests:

    cd /path/to/your/python/app
    pytest
    

How It Works

  • The tool collects all Python files in your app.
  • Smart caching: Before making API calls, it checks if any files have changed since the last run by comparing file hashes and modification times. If no changes are detected, it skips the API call to save costs.
  • It sends the code to Claude AI with a prompt to generate pytest-style tests.
  • The generated tests are written to tests/test_generated.py (or one test file per source file).
  • Each test file includes a snippet to patch sys.path for import safety.
  • A .test_generator_cache file is created in your app directory to track file changes.

Advanced: Isolating File-Based Storage in Tests

If your app uses a file (like tasks.json) for storage, the tool can generate a pytest fixture to patch the storage path using tmp_path and monkeypatch. This ensures each test runs in isolation and avoids data leakage between tests.

Example fixture (auto-included if detected):

import pytest

@pytest.fixture
def temp_tasks_file(tmp_path, monkeypatch):
    temp_file = tmp_path / "tasks.json"
    monkeypatch.setattr("notes_app.storage.TASKS_FILE", str(temp_file))
    return str(temp_file)

Troubleshooting

  • ModuleNotFoundError:
    Make sure you run pytest from your project root, and that the generated test files include the sys.path patch at the top.
  • API Errors:
    Ensure your ANTHROPIC_API_KEY is set and valid.
  • No tests generated:
    Check that your app directory contains .py files and is accessible.
  • Tool says "no changes detected" but you want to regenerate:
    Delete the .test_generator_cache file in your app directory and run the tool again.

License

MIT License


Credits

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

testcases_writer_ai_tool-0.1.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

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

testcases_writer_ai_tool-0.1.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file testcases_writer_ai_tool-0.1.0.tar.gz.

File metadata

  • Download URL: testcases_writer_ai_tool-0.1.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for testcases_writer_ai_tool-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f5b98c50d8a5a8f1e1c93629a392af0d1313c1f1872f08256576561df21a13fb
MD5 f6a042a4d523136a3f8196ce6934bd5e
BLAKE2b-256 538be9b51fc683e05132ac6efad36ec35c54c4a5c6b4dcfebc829ca8fe5522a7

See more details on using hashes here.

File details

Details for the file testcases_writer_ai_tool-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for testcases_writer_ai_tool-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f3596bc2c230cbeb5cd55944eb3a8ba5fc58a13651d26797d12a77aa221bbd53
MD5 ba3746814beab746af664e639e22ed77
BLAKE2b-256 7316abca1fb35c22d5833de463ddf62f897bd605d692ae5044f2bfbb8f57f476

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