By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
Reading: Let AI Write Your Tests: How I Used OpenAI and Pytest to Auto-Generate API Test Cases | HackerNoon
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
World of Software > Computing > Let AI Write Your Tests: How I Used OpenAI and Pytest to Auto-Generate API Test Cases | HackerNoon
Computing

Let AI Write Your Tests: How I Used OpenAI and Pytest to Auto-Generate API Test Cases | HackerNoon

News Room
Last updated: 2025/12/03 at 8:31 AM
News Room Published 3 December 2025
Share
Let AI Write Your Tests: How I Used OpenAI and Pytest to Auto-Generate API Test Cases | HackerNoon
SHARE

AI seems to help almost every job in some way.

As SDET / QA, what if your test cases could be auto-generated by AI and automatically verified using Pytest ?

Hence, I recently tried integrating OpenAI with Pytest to automate API test generation. Here’s what I learned.

What You Need to Know and Prepare

Before diving in, here some tools and resources you’ll need:

  • OpenAI Python Library n https://github.com/openai/openai-python

  • Pytest (Python testing framework) n https://github.com/pytest-dev/pytest

  • API Under Test — FakeStoreAPI (Cart endpoint)

    https://fakestoreapi.com/docs#tag/Carts/operation/addCart

Setup Overview

Let OpenAI Build and Verify Basic Test Cases

We’ll start by asking OpenAI to generate simple API test cases using structured prompts. Then, we use Pytest to run and validate those cases against the real API.

  1. Install and Import Required Packages
$ pip install openai
$ pip install pytest

https://gist.github.com/shyinlim/76bc4b8a37df414cccf29ea227ef6ab4?embedable=true

2. Define the Test Case Generator Function

We’ll create a function to send a prompt to OpenAI, instructing it to behave like a Senior SDET / QA and return structured API test cases in JSON format.

https://gist.github.com/shyinlim/7190ab613a342ce77c94bc906a55e258?embedable=true

3. Let OpenAI Build and Verify Basic Test Cases

Before defining the test function, we need to prepare a prompt that describes the API’s method, endpoint, and a sample of the request and response structure. This prompt will guide OpenAI to generate the test cases.

https://gist.github.com/shyinlim/1f6fe4fa0cc99edcff9e82504164ebfd?embedable=true

Once the prompt is ready, we define a test function that sends this information to OpenAI, retrieves the generated test cases, and runs them using Pytest.

https://gist.github.com/shyinlim/58334767807f41f76d5fbf73b4ac1f60?embedable=true

Okay, here’s a sample test cases of what OpenAI might return:

  • createcartwithvalidsingle_product
  • createcartwithmultipleproducts
  • createcartwithminimumvalid_values

Expand with Human-Designed Cases

While AI can quickly generate basic test cases. However, edge cases, business-specific logic, or tricky validation rules often still require human insight.

Use AI as a starting point, then build on it with your domain knowledge.

https://gist.github.com/shyinlim/709a54c3b9491a8f2f0b6aa171145211?embedable=true

Knowing When to Let AI Handle It

AI-generated test functions work best when:

  • API specs are clear and well-defined, including methods, parameters, and expected responses
  • Field validations are needed (data types, required fields, ranges).
  • Request / response flows are standard without complex business logic.
  • Need to quickly bootstrap test coverage, especially for repetitive or CRUD endpoints

You’ll still want to write tests manually when:

  • Business logic is complex or conditional, which AI may not fully understand.
  • Heavily DB-dependent tests, they often require manual setup and domain knowledge to ensure correctness.
  • Mocking or stubbing other services is needed, especially async or dependent ones.
  • Test results depend on side effects beyond response values, like logs or DB updates.
  • Security testing such as auth, permission, or injection checks is involved.

Final Thoughts

This experiment shows a great way to boost testing efficiency by combining OpenAI and Pytest.

It’s not about replacing SDET / QA Engineers, but about helping us get started faster, cover more ground, and focus on the tricky stuff.

The key takeaway ? n The magic isn’t just in the AI, it’s in the prompt.

Good prompts don’t just show up by magic, they come from your mix of :

  • Testing experience
  • Testing techniques
  • Testing mindset
  • Communication skills
  • Product understanding

Here’s my GitHub repo showing a straightforward example:

https://github.com/shyinlim/openaiwithpytestsimpleversion?source=postpage—–77e9cdb5b716—————————————&embedable=true

Test smart, not hard. And, Happy Testing 🙂

Sh-Yin Lim (LinkedIn)

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article DoorDash Has A New Robot That Can Roll Right Up To Your Door – BGR DoorDash Has A New Robot That Can Roll Right Up To Your Door – BGR
Next Article DRAM it! Raspberry Pi raises prices DRAM it! Raspberry Pi raises prices
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

Stop Guessing Your Rates: How Filipino Web Devs Can Price Projects Like Pros | HackerNoon
Stop Guessing Your Rates: How Filipino Web Devs Can Price Projects Like Pros | HackerNoon
Computing
India revokes order to preload smartphones with state-owned security app
India revokes order to preload smartphones with state-owned security app
News
The Color of Your Sponge Matters. Here's How to Choose the Right One
The Color of Your Sponge Matters. Here's How to Choose the Right One
News
How Vove ID is tackling Africa’s KYC and KYB headaches |
How Vove ID is tackling Africa’s KYC and KYB headaches |
Computing

You Might also Like

Stop Guessing Your Rates: How Filipino Web Devs Can Price Projects Like Pros | HackerNoon
Computing

Stop Guessing Your Rates: How Filipino Web Devs Can Price Projects Like Pros | HackerNoon

7 Min Read
How Vove ID is tackling Africa’s KYC and KYB headaches |
Computing

How Vove ID is tackling Africa’s KYC and KYB headaches |

13 Min Read
Fighting Context Rot in Long-Context LLMs | HackerNoon
Computing

Fighting Context Rot in Long-Context LLMs | HackerNoon

3 Min Read
New fund offers ,000 grants for tech-enabled urban solutions in Africa
Computing

New fund offers $75,000 grants for tech-enabled urban solutions in Africa

4 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright © All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?