Python Library to generate unit tests using OpenAI API
python -m venv venv
source venv/bin/activate # venv/Scripts/activate on Windows
pip install aitestgen
export OPENAI_API_KEY=<my_api_key> # or add variable in your .env file
Create a python file and add the following code:
from aitestgen.autotest import autotest
@autotest()
def sum(num1: float, num2: float) -> float:
"""This function is responsible for calculating the sum of two numbers"""
return num1 + num2
@autotest()
def mul(num1: float, num2: float) -> float:
"""This function is responsible for calculating the multiplication of two numbers"""
return num1 * num2
To improve results, add type annotations and documentation to the function.
Run the following command (change the filename)
aitestgen generate --inputfile src/operations.py
And then you will get the following result in a .py file created in a tests folder:
# Function: mul - test1:
def test_mul_positive_numbers():
assert mul(5.5, 2) == 11.0
# Function: mul - test2:
def test_mul_negative_numbers():
assert mul(-3, 4) == -12.0
# Function: mul - test3:
def test_mul_zero():
assert mul(0, 10) == 0.0
# Function: sum - test1:
def test_sum_positive_numbers():
assert sum(3.5, 2.5) == 6.0
# Function: sum - test2:
def test_sum_negative_numbers():
assert sum(-3.5, -2.5) == -6.0
# Function: sum - test3:
def test_sum_mixed_numbers():
assert sum(3.5, -2.5) == 1.0