关注留言点赞,带你了解最流行的软件开发知识与最新科技行业趋势。
ChatGPT 可以帮助您在 REST Assured 中生成 API 测试!这篇文章探讨了可能会破坏测试环境的新人工智能技术的力量。
ChatGPT 可以帮助您在REST Assured中生成API 测试!
ChatGPT是一种可以理解和回复人类输入的人工智能,就像一个可以与之对话的高智能机器人。只需免费开设一个帐户,然后像与朋友一样开始聊天。
让我们首先请求 ChatGPT 生成一个 API 规范,以便我们可以展示它稍后如何创建测试。
我对 ChatGPT:“为一家名为 FoxShipping 的新航运公司的跟踪 API 创建一个 OpenAPI 规范。”
它为我们的新 FoxShipping 跟踪 API生成示例OpenAPI规范:
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: support@foxshipping.com
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
现在,我们可以要求它为它创建 API 测试。
我对 ChatGPT:“使用 RestAssured 为 FoxShipping Tracking API 编写 API 测试。”
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"));
}
}
看起来不错,但我们也需要涵盖错误情况。我们跟进。
我对 ChatGPT 说:“还请为 错误场景添加测试。”
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"));
}
}
这样,我们现在有 3 个测试用例,涵盖了 FoxShipping 跟踪服务 OpenAPI 规范中概述的所有 3 个场景。这些为您奠定了坚实的基础,您可以将其用作起点并通过添加您自己的自定义项和其他测试用例来构建。
您还想了解更多关于在测试中使用 ChatGPT 的其他主题吗?请在下面的评论中留下您的建议,我会写下来。
附注:我使用来自 OpenAPI 的 DALL-E 2为这篇文章生成了您在标题中看到的图像,方法是要求它“与人类开发人员进行 AI 实体对编程”。这个很不错!
请注意,出于安全原因,不建议将您公司的私有专有代码粘贴到 ChatGPT 中,除非您有充分的理由并获得这样做的许可! 本文的目的是展示可能会破坏测试环境的新 AI 技术的强大功能。如果您正在为您的团队寻找特定的测试生成方法,请随时直接与我联系。
领取专属 10元无门槛券
私享最新 技术干货