Generate automated API tests with ChatGPT in seconds
DALL-E 2 OpenAPI generated

Generate automated API tests with ChatGPT in seconds

You can use ChatGPT to generate API tests in RestAssured!

ChatGTP is an AI technology that can understand and respond to things people say, like a really smart robot you can talk to. Just go to https://chat.openai.com/chat open an account for free and start chatting with it like with a friend.

Let's start by asking ChatGPT to create an API specification for us to be able to demonstrate how it can create the tests later on as well.

Me to ChatGPT: "Create an OpenAPI specification for a tracking API of a new shipping company called FoxShipping."

It generates a sample OpenAPI spec for our new FoxShipping tracking API:

openapi: 3.0.
info:
? title: FoxShipping Tracking API
? version: 1.0.0
? description: This API allows customers to track their shipments with FoxShipping.
? contact:
? ? name: FoxShipping Support
? ? email: [email protected]
? ? url: https://www.foxshipping.com
servers:
? - url: https://api.foxshipping.com/tracking
paths:
? /:
? ? post:
? ? ? summary: Track a shipment with FoxShipping
? ? ? operationId: trackShipment
? ? ? requestBody:
? ? ? ? required: true
? ? ? ? content:
? ? ? ? ? application/json:
? ? ? ? ? ? schema:
? ? ? ? ? ? ? type: object
? ? ? ? ? ? ? properties:
? ? ? ? ? ? ? ? trackingNumber:
? ? ? ? ? ? ? ? ? type: string
? ? ? ? ? ? ? ? ? example: 1234567890
? ? ? ? ? ? ? ? apiKey:
? ? ? ? ? ? ? ? ? type: string
? ? ? ? ? ? ? ? ? example: YOUR_API_KEY
? ? ? responses:
? ? ? ? 200:
? ? ? ? ? description: Successful tracking request
? ? ? ? ? content:
? ? ? ? ? ? application/json:
? ? ? ? ? ? ? schema:
? ? ? ? ? ? ? ? type: object
? ? ? ? ? ? ? ? properties:
? ? ? ? ? ? ? ? ? trackingNumber:
? ? ? ? ? ? ? ? ? ? type: string
? ? ? ? ? ? ? ? ? ? example: 1234567890
? ? ? ? ? ? ? ? ? status:
? ? ? ? ? ? ? ? ? ? type: string
? ? ? ? ? ? ? ? ? ? example: "Delivered"
? ? ? ? ? ? ? ? ? estimatedDelivery:
? ? ? ? ? ? ? ? ? ? type: string
? ? ? ? ? ? ? ? ? ? format: date-time
? ? ? ? ? ? ? ? ? ? example: "2022-01-01T12:00:00Z"
? ? ? ? 401:
? ? ? ? ? description: Unauthorized request
? ? ? ? 404:
? ? ? ? ? description: Tracking number not found        

Now, we can ask it to create the API tests for it.

Me to ChatGPT: "Write an API test for FoxShipping Tracking API using RestAssured."

import io.restassured.RestAssured
import io.restassured.response.Response;
import org.junit.BeforeClass;
import org.junit.Test;


public class FoxShippingTrackingAPITest {
? ? private static final String API_KEY = "YOUR_API_KEY";
? ? @BeforeClass
? ? public static void setup() {
? ? ? ? RestAssured.baseURI = "https://api.foxshipping.com/tracking";
? ? }


? ? @Test
? ? public void testTrackShipment() {
? ? ? ? String trackingNumber = "1234567890";
? ? ? ? String requestBody = "{\"trackingNumber\":\"" + trackingNumber + "\",\"apiKey\":\"" + API_KEY + "\"}";
? ? ? ? Response response = RestAssured.given().
? ? ? ? ? ? ? ? contentType("application/json").
? ? ? ? ? ? ? ? body(requestBody).
? ? ? ? ? ? ? ? when().
? ? ? ? ? ? ? ? post();
? ? ? ? int statusCode = response.getStatusCode();
? ? ? ? assertEquals(200, statusCode);
? ? ? ? String responseBody = response.getBody().asString();
? ? ? ? assertTrue(responseBody.contains(trackingNumber));
? ? ? ? assertTrue(responseBody.contains("status"));
? ? ? ? assertTrue(responseBody.contains("estimatedDelivery"));
? ? }
}        

Looks decent, but we need to cover the error cases as well. We follow up.

Me to ChatGPT: "Please also add the tests for the error scenarios."

import io.restassured.RestAssured
import io.restassured.response.Response;
import org.junit.BeforeClass;
import org.junit.Test;


public class FoxShippingTrackingAPITest {
? ? private static final String API_KEY = "YOUR_API_KEY";
? ? @BeforeClass
? ? public static void setup() {
? ? ? ? RestAssured.baseURI = "https://api.foxshipping.com/tracking";
? ? }


? ? @Test
? ? public void testTrackShipment() {
? ? ? ? String trackingNumber = "1234567890";
? ? ? ? String requestBody = "{\"trackingNumber\":\"" + trackingNumber + "\",\"apiKey\":\"" + API_KEY + "\"}";
? ? ? ? Response response = RestAssured.given().
? ? ? ? ? ? ? ? contentType("application/json").
? ? ? ? ? ? ? ? body(requestBody).
? ? ? ? ? ? ? ? when().
? ? ? ? ? ? ? ? post();
? ? ? ? int statusCode = response.getStatusCode();
? ? ? ? assertEquals(200, statusCode);
? ? ? ? String responseBody = response.getBody().asString();
? ? ? ? assertTrue(responseBody.contains(trackingNumber));
? ? ? ? assertTrue(responseBody.contains("status"));
? ? ? ? assertTrue(responseBody.contains("estimatedDelivery"));
? ? }
? ??
? ? @Test
? ? public void testUnauthorizedRequest() {
? ? ? ? String trackingNumber = "1234567890";
? ? ? ? String requestBody = "{\"trackingNumber\":\"" + trackingNumber + "\",\"apiKey\":\"INVALID_API_KEY\"}";
? ? ? ? Response response = RestAssured.given().
? ? ? ? ? ? ? ? contentType("application/json").
? ? ? ? ? ? ? ? body(requestBody).
? ? ? ? ? ? ? ? when().
? ? ? ? ? ? ? ? post();
? ? ? ? int statusCode = response.getStatusCode();
? ? ? ? assertEquals(401, statusCode);
? ? ? ? String responseBody = response.getBody().asString();
? ? ? ? assertTrue(responseBody.contains("Unauthorized request"));
? ? }


? ? @Test
? ? public void testTrackingNumberNotFound() {
? ? ? ? String trackingNumber = "INVALID_TRACKING_NUMBER";
? ? ? ? String requestBody = "{\"trackingNumber\":\"" + trackingNumber + "\",\"apiKey\":\"" + API_KEY + "\"}";
? ? ? ? Response response = RestAssured.given().
? ? ? ? ? ? ? ? contentType("application/json").
? ? ? ? ? ? ? ? body(requestBody).
? ? ? ? ? ? ? ? when().
? ? ? ? ? ? ? ? post();
? ? ? ? int statusCode = response.getStatusCode();
? ? ? ? assertEquals(404, statusCode);
? ? ? ? String responseBody = response.getBody().asString();
? ? ? ? assertTrue(responseBody.contains("Tracking number not found"));
? ? }
}        

So, now we have 3 test cases covering all 3 scenarios described in the FoxShipping Tracking service OpenAPI specification. They are a pretty good starting point to base your actual tests on and add your customisation and additional test cases.

What would you like to read more about next when it comes to using ChatGPT in testing? Leave it in a comment below, and I will write about it!

P.S. I generated the image for this article you have seen in the header using DALL-E 2 from OpenAPI by asking it for "An AI entity pair programming?with a human developer". It's pretty good!

Please note that it is not recommended to paste your company's private proprietary code into ChatGPT for security reasons unless you have a good reason and permission to do so!

Thanks for the valuable info!?In the area of AI-assisted testing, I would like to read more about the Reflect platform (https://reflect.run).

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了