Claude Test Generator
A Python library and CLI tool that uses Claude AI to auto-generate unit tests for Python functions or classes. This tool aids developers in quickly generating robust test cases, saving time and improving test coverage.
Installation
To install the required dependencies, run:
pip install openai click pytestUsage
You can use the tool via the command line. The tool requires an OpenAI API key to interact with Claude AI.
Command Line Interface
python claude_test_generator.py --input <path_to_python_file> --output <path_to_output_test_file> --api-key <your_openai_api_key>--input: Path to the Python file you want to analyze.--output: Path where the generated test file will be saved.--api-key: Your OpenAI API key for accessing Claude AI.
Example
python claude_test_generator.py --input example.py --output test_example.py --api-key YOUR_API_KEYThis will generate a test_example.py file containing unit tests for the code in example.py.
Source Code
import os
import openai
import click
def generate_tests_with_claude(api_key, code_snippet):
"""
Generate unit tests for the given Python code snippet using Claude AI.
Args:
api_key (str): OpenAI API key for Claude.
code_snippet (str): Python code snippet to analyze.
Returns:
str: Generated unit test code.
"""
openai.api_key = api_key
prompt = (
"You are an AI that generates Python unit tests. "
"Given the following Python code, write unit tests using pytest to cover its functionality. "
"Include edge cases and error handling scenarios.\n\n"
f"Code:\n{code_snippet}\n\n"
"Unit Tests:"
)
try:
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=1500,
temperature=0.7
)
return response.choices[0].text.strip()
except openai.error.OpenAIError as e:
raise RuntimeError(f"Error communicating with Claude AI: {e}")
@click.command()
@click.option('--input', 'input_path', required=True, type=click.Path(exists=True), help="Path to the Python file to analyze.")
@click.option('--output', 'output_path', required=True, type=click.Path(), help="Path to save the generated test file.")
@click.option('--api-key', 'api_key', required=True, help="OpenAI API key for Claude.")
def main(input_path, output_path, api_key):
"""
CLI entry point for the Claude Test Generator.
Args:
input_path (str): Path to the Python file to analyze.
output_path (str): Path to save the generated test file.
api_key (str): OpenAI API key for Claude.
"""
try:
with open(input_path, 'r') as f:
code_snippet = f.read()
generated_tests = generate_tests_with_claude(api_key, code_snippet)
with open(output_path, 'w') as f:
f.write(generated_tests)
click.echo(f"Test cases generated and saved to {output_path}")
except FileNotFoundError:
click.echo("Error: Input file not found.", err=True)
except RuntimeError as e:
click.echo(f"Error: {e}", err=True)
except Exception as e:
click.echo(f"An unexpected error occurred: {e}", err=True)
if __name__ == "__main__":
main()
README
Claude Test Generator
Claude Test Generator is a Python library and CLI tool that uses Claude AI to auto-generate unit tests for Python functions or classes. This tool aids developers in quickly generating robust test cases, saving time and improving test coverage.
Installation
To install the required dependencies, run:
pip install openai click pytestUsage
You can use the tool via the command line. The tool requires an OpenAI API key to interact with Claude AI.
Command Line Interface
python claude_test_generator.py --input <path_to_python_file> --output <path_to_output_test_file> --api-key <your_openai_api_key>--input: Path to the Python file you want to analyze.--output: Path where the generated test file will be saved.--api-key: Your OpenAI API key for accessing Claude AI.
Example
python claude_test_generator.py --input example.py --output test_example.py --api-key YOUR_API_KEYThis will generate a test_example.py file containing unit tests for the code in example.py.
Testing
To run the tests, use pytest:
pytest test_claude_test_generator.pyThe tests include mocking for the OpenAI API, so no network access is required.
License
This project is licensed under the MIT License.
Community
Downloads
ยทยทยท
Rate this tool
No ratings yet โ be the first!
Details
- Tool Name
- claude_test_generator
- Category
- Claude AI for Coding
- Generated
- April 26, 2026
- Tests
- Passing โ
- Fix Loops
- 3
Quick Install
Clone just this tool:
git clone --depth 1 --filter=blob:none --sparse \ https://github.com/ptulin/autoaiforge.git cd autoaiforge git sparse-checkout set generated_tools/2026-04-26/claude_test_generator cd generated_tools/2026-04-26/claude_test_generator pip install -r requirements.txt 2>/dev/null || true python claude_test_generator.py