How to beginner · 3 min read

How to use AI to generate unit tests

Quick answer
Use an AI model like gpt-4o to generate unit tests by providing it with your source code and a prompt requesting test cases. The AI returns test code snippets you can integrate into your test suite, automating test creation and improving coverage.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the OpenAI Python SDK and set your API key as an environment variable to authenticate requests.

bash
pip install openai>=1.0

Step by step

Provide your source code and a clear prompt to the gpt-4o model to generate unit tests. The example below shows a complete script that sends a Python function and asks the AI to write pytest unit tests for it.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

source_code = '''
def add(a, b):
    """Return the sum of two numbers."""
    return a + b
'''

prompt = f"""Generate pytest unit tests for the following Python function:\n{source_code}\n"""

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": prompt}]
)

print(response.choices[0].message.content)
output
import pytest

from your_module import add

def test_add_positive_numbers():
    assert add(2, 3) == 5

def test_add_negative_numbers():
    assert add(-1, -1) == -2

def test_add_zero():
    assert add(0, 0) == 0

Common variations

  • Use claude-3-5-sonnet-20241022 for potentially better code generation quality.
  • Generate tests asynchronously using async SDK calls.
  • Customize prompts to generate tests for edge cases or specific frameworks like unittest or nose.
python
import os
import asyncio
from openai import OpenAI

async def generate_tests_async():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    source_code = '''\ndef multiply(x, y):\n    return x * y\n'''
    prompt = f"Generate unittest test cases for this function:\n{source_code}\n"

    response = await client.chat.completions.acreate(
        model="gpt-4o",
        messages=[{"role": "user", "content": prompt}]
    )
    print(response.choices[0].message.content)

asyncio.run(generate_tests_async())
output
import unittest

from your_module import multiply

class TestMultiply(unittest.TestCase):
    def test_multiply_positive(self):
        self.assertEqual(multiply(3, 4), 12)

    def test_multiply_zero(self):
        self.assertEqual(multiply(0, 5), 0)

Troubleshooting

  • If the AI generates incomplete or incorrect tests, refine your prompt with more context or examples.
  • Ensure your API key is correctly set in os.environ["OPENAI_API_KEY"] to avoid authentication errors.
  • Check for rate limits or quota issues if requests fail unexpectedly.

Key Takeaways

  • Use clear, specific prompts including source code to get accurate unit test generation from AI.
  • The gpt-4o model is effective for generating Python unit tests with minimal setup.
  • Async calls and alternative models like claude-3-5-sonnet-20241022 offer flexibility and potentially higher quality.
  • Always validate AI-generated tests manually before integrating into your CI pipeline.
  • Proper environment setup and prompt engineering are key to reliable AI test generation.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗