๐ง AI-Powered Coding AssistantsApril 30, 2026โ
Tests passing
AI Test Case Generator
This CLI tool utilizes advanced AI models to automatically generate pytest test cases for a given Python module or function. It reduces the burden of writing repetitive tests while promoting better code coverage.
What It Does
- Generate pytest test cases for an entire Python file or a specific function.
- Save the generated test cases to a specified output file.
- Supports parameterized tests and includes comments for better understanding.
Installation
1. Clone the repository:
git clone <repository_url>
cd ai_test_case_generator2. Install the required dependencies:
pip install -r requirements.txtUsage
To use the AI Test Case Generator, run the following command:
python ai_test_case_generator.py --file <path_to_python_file> --output <path_to_output_file> --api-key <your_openai_api_key>Options
--file: Path to the Python file for which you want to generate test cases (required).--function: (Optional) Specify a function name to generate test cases only for that function.--output: Path to save the generated test cases (required).--api-key: Your OpenAI API key (required). You can also set this as an environment variableOPENAI_API_KEY.
Example
Generate test cases for a Python file:
python ai_test_case_generator.py --file example.py --output test_example.py --api-key YOUR_API_KEYGenerate test cases for a specific function in a Python file:
python ai_test_case_generator.py --file example.py --function my_function --output test_example.py --api-key YOUR_API_KEYSource Code
import os
import openai
import click
def generate_test_cases(api_key, file_path, function_name=None):
"""
Generate pytest test cases for a given Python file or function.
Args:
api_key (str): OpenAI API key.
file_path (str): Path to the Python file.
function_name (str, optional): Specific function to generate tests for. Defaults to None.
Returns:
str: Generated pytest test cases as a string.
"""
if not os.path.exists(file_path):
raise FileNotFoundError(f"The file '{file_path}' does not exist.")
with open(file_path, 'r') as file:
code_content = file.read()
prompt = f"""
You are an expert Python developer. Generate pytest test cases for the following Python code.
Include parameterized tests where applicable and add comments explaining each test case.
Code:
{code_content}
"""
if function_name:
prompt += f"Focus on the function named '{function_name}'."
openai.api_key = api_key
try:
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=1500,
temperature=0.7
)
return response["choices"][0]["text"].strip()
except Exception as e:
raise RuntimeError(f"Failed to generate test cases: {e}")
@click.command()
@click.option('--file', 'file_path', required=True, type=click.Path(exists=True), help="Path to the Python file.")
@click.option('--function', 'function_name', required=False, help="Specific function to generate tests for.")
@click.option('--output', 'output_file', required=True, type=click.Path(), help="Path to save the generated test file.")
@click.option('--api-key', 'api_key', envvar='OPENAI_API_KEY', required=True, help="OpenAI API key.")
def main(file_path, function_name, output_file, api_key):
"""
CLI entry point for the AI Test Case Generator.
"""
try:
test_cases = generate_test_cases(api_key, file_path, function_name)
with open(output_file, 'w') as f:
f.write(test_cases)
click.echo(f"Test cases generated and saved to {output_file}")
except Exception as e:
click.echo(f"Error: {e}", err=True)
if __name__ == "__main__":
main()
Community
Downloads
ยทยทยท
Rate this tool
No ratings yet โ be the first!
Details
- Tool Name
- ai_test_case_generator
- Category
- AI-Powered Coding Assistants
- Generated
- April 30, 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-30/ai_test_case_generator cd generated_tools/2026-04-30/ai_test_case_generator pip install -r requirements.txt 2>/dev/null || true python ai_test_case_generator.py