How to Beginner to Intermediate · 3 min read

How to use AI to create test cases from requirements

Quick answer
Use a large language model like gpt-4o to parse software requirements and generate structured test cases by prompting it with clear instructions. Call the chat.completions.create API with your requirements as input and ask the model to output test cases in a defined format.

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

Write a Python script that sends your software requirements to the gpt-4o model with a prompt instructing it to generate test cases. The model will return structured test cases you can use directly.

python
import os
from openai import OpenAI

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

requirements = """
The system shall allow users to log in with a username and password.
Passwords must be at least 8 characters long.
The system shall lock the account after 3 failed login attempts.
"""

prompt = f"Generate detailed test cases from the following requirements:\n{requirements}\nFormat each test case with a title, steps, expected results."

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

test_cases = response.choices[0].message.content
print(test_cases)
output
Test Case 1: User Login with Valid Credentials
Steps:
1. Navigate to the login page.
2. Enter a valid username.
3. Enter a valid password (at least 8 characters).
4. Click the login button.
Expected Result:
User is successfully logged in and redirected to the dashboard.

Test Case 2: Password Length Validation
Steps:
1. Navigate to the login page.
2. Enter a valid username.
3. Enter a password shorter than 8 characters.
4. Click the login button.
Expected Result:
An error message is displayed indicating the password is too short.

Test Case 3: Account Lockout After Failed Attempts
Steps:
1. Navigate to the login page.
2. Enter a valid username.
3. Enter an incorrect password.
4. Repeat step 3 two more times.
5. Attempt to log in again with the correct password.
Expected Result:
Account is locked after 3 failed attempts; login is denied even with correct password.

Common variations

You can use asynchronous calls for better performance or try different models like claude-3-5-sonnet-20241022 for potentially improved test case generation quality. Streaming responses can also be enabled for real-time output.

python
import os
import asyncio
from openai import OpenAI

async def generate_test_cases():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    requirements = "The system shall send email notifications on password reset."
    prompt = f"Generate test cases from these requirements:\n{requirements}\nInclude steps and expected results."

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

asyncio.run(generate_test_cases())
output
Test Case: Email Notification on Password Reset
Steps:
1. Request a password reset.
2. Check the registered email inbox.
Expected Result:
An email notification with password reset instructions is received.

Troubleshooting

  • If the model returns vague or incomplete test cases, refine your prompt to be more explicit about format and detail.
  • If you get authentication errors, verify your API key is correctly set in os.environ["OPENAI_API_KEY"].
  • For rate limits, consider batching requirements or upgrading your API plan.

Key Takeaways

  • Use clear, structured prompts to guide AI in generating detailed test cases from requirements.
  • The gpt-4o model via OpenAI SDK v1+ is effective for automated test case generation.
  • Async calls and alternative models like claude-3-5-sonnet-20241022 can improve performance and quality.
  • Always handle API keys securely via environment variables to avoid authentication issues.
  • Refine prompts iteratively to get precise and actionable test cases.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗