Unittest AI Agent is a Python tool that automatically generates comprehensive unit tests for your Python functions and classes using OpenAI's GPT models. It analyzes your source code, prepares context-rich prompts, and writes robust pytest-based test suites.
- Automatic Source Analysis: Extracts imports, functions, and class context from your Python files.
- Prompt Engineering: Uses customizable prompt templates for both standalone functions and class methods.
- LLM Integration: Sends code context to OpenAI GPT-4o to generate high-quality unit tests.
- Test Postprocessing: Cleans and adapts generated code for your project structure.
- Test Writing: Saves generated tests to the appropriate directory.
-
Requirements:
- Python 3.9 or higher
- OpenAI API key
- Poetry
curl -sSL https://install.python-poetry.org | python3
-
Install dependencies:
poetry install
-
Set your OpenAI API key:
- Rename the
.env.exampleto.env:OPENAI_API_KEY=your_openai_api_key_here
- Rename the
-
Run the test generator:
poetry run ut generate example/converter.py
This will analyze
ut/example/converter.pyand generate tests inut/example/tests/.
- Prompt Templates: Edit files in
ut/prompts/to change how prompts are constructed for the LLM.
Given a function like:
def convert_date_to_iso(date_str: str, format: str = "%d/%m/%Y") -> str:
...The agent will generate a suite of pytest tests covering various edge cases and save them to ut_output/test_convert.py.
This project is under active development. Below is a summary of our progress and a look at what's ahead.
Contributions are highly encouraged!
๐ Roadmap: https://focusmap.pro/roadmap/45a1b599-aead-4c11-b749-032e5ea168e1
Apache 2.0