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 withpip install -r requirements.txt)
Installation
-
Clone this repository (or copy the tool files):
git clone <your-repo-url> cd TestCases_Writer_AI_Tool
-
Install dependencies:
pip install -r requirements.txt
-
Set your Anthropic API key:
export ANTHROPIC_API_KEY=sk-ant-...
Usage
-
Run the tool on your Python app:
generate-test-cases /path/to/your/python/app- Replace
/path/to/your/python/appwith the path to the root of your Python project. - The tool will scan all
.pyfiles, send them to Claude, and write generated tests to atests/directory inside your app.
- Replace
-
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.pathfor import safety. - A
.test_generator_cachefile 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 runpytestfrom your project root, and that the generated test files include thesys.pathpatch at the top. - API Errors:
Ensure yourANTHROPIC_API_KEYis set and valid. - No tests generated:
Check that your app directory contains.pyfiles and is accessible. - Tool says "no changes detected" but you want to regenerate:
Delete the.test_generator_cachefile in your app directory and run the tool again.
License
MIT License
Credits
- Built with Anthropic Claude API.
- Test generation logic by [your name or org].
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5b98c50d8a5a8f1e1c93629a392af0d1313c1f1872f08256576561df21a13fb
|
|
| MD5 |
f6a042a4d523136a3f8196ce6934bd5e
|
|
| BLAKE2b-256 |
538be9b51fc683e05132ac6efad36ec35c54c4a5c6b4dcfebc829ca8fe5522a7
|
File details
Details for the file testcases_writer_ai_tool-0.1.0-py3-none-any.whl.
File metadata
- Download URL: testcases_writer_ai_tool-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f3596bc2c230cbeb5cd55944eb3a8ba5fc58a13651d26797d12a77aa221bbd53
|
|
| MD5 |
ba3746814beab746af664e639e22ed77
|
|
| BLAKE2b-256 |
7316abca1fb35c22d5833de463ddf62f897bd605d692ae5044f2bfbb8f57f476
|