Tests
您可以为API请求编写测试脚本,语言为javaScript
当您引入新代码时,测试会确保您的API按预期工作。你的测试覆盖率越高,你的代码就越灵活,越能抵抗bug,你就越少花时间思考为什么椰子的图片破坏了你的代码。
编写 Tests
在上一节(Pre-request Scripts)中,我们处理了动态修改请求。在此,我们将处理收到的响应。
pw API
PostPanda有一个名为pw的API,用于处理预请求和测试。
.expect(value)
expect返回一个expectation对象,您可以在该对象上调用匹配函数。
下面的示例中,expectation对象调用pw.expect使用response id(pw.response.body.id)作为参数,调用匹配函数toBe。
直接使用pw.expect进行快速方便的测试。每个pw.expect语句都会在测试报告上生成一行。
// This test will pass
pw.expect(1).toBe(1);
// This test will fail
pw.expect(2).not.toBe(2);
.test(name,fn)
创建一组测试,名称为字符串,fn为回调函数,以编写与该组关联的测试。测试结果将包括给定的名称,以便更好地组织。
用pw.test封装expect语句,以对相关语句进行分组和描述。
// This will return 4 lines on the test report, grouped under "Arithmetic operations"
pw.test("Arithmetic operations", () => {
const size = 500 + 500;
pw.expect(size).toBe(1000);
pw.expect(size - 500).toBe(500);
pw.expect(size * 4).toBe(4000);
pw.expect(size / 4).toBe(250);
});
如果pw.expect和pw.test语句都不存在,则不会生成测试报告。
// This will not generate any test reports
(99 + 1).toBe(100);
但是,不能在.test回调函数中嵌套.test。
pw.test("a group of tests", () => {
pw.expect(10).toBe(10);
// more tests here
});
.toBe(value)
使用toBe测试精确相等性。
pw.expect(pw.response.body.category).toBe("Sneakers");
toBe使用严格相等,建议用于基本数据类型。
// These tests will fail
pw.expect("hello").toBe("Hello");
pw.expect(5).toBe("5");
pw.expect([]).toBe([]);
.not
在调用匹配函数之前,通过添加.not来测试不匹配。
// These tests will pass
pw.expect(true).not.toBe(false);
pw.expect(200).not.toBeLevel3xx();
.toBeLevelxxx()
有四种不同的匹配器可快速方便地测试返回的http状态代码:
toBeLevel2xx()toBeLevel3xx()toBeLevel4xx()toBeLevel5xx()
例如,传递给 expect 的参数必须在200到299之间(含200和299)才能传递给 BeLevel2xx()。
// These tests will pass
pw.expect(204).toBeLevel2xx();
pw.expect(308).toBeLevel3xx();
pw.expect(404).toBeLevel4xx();
pw.expect(503).toBeLevel5xx();
如果传递给 expect 的参数是非数值,则首先使用 parseInt() 对其进行分析。
// This test will pass
pw.expect(parseInt("404")).toBeLevel4xx();
.toBeType(type)
使用 .toBeType(type) 进行类型检查。此方法的参数应为"string", "boolean", "number", "object",
"undefined", "bigint", "symbol" or "function".
// These tests will pass
pw.expect(5).toBeType("number");
pw.expect("Hello, world!").toBeType("string");
pw.expect(5).not.toBeType("string");
pw.expect("Hello, world!").not.toBeType("number");
.toHaveLength(number)
使用 .toHaveLength(number) 检查对象是否具有 .length 属性,并将其设置为特定的数值。
// These expectations will pass
pw.expect("gmpost").toHaveLength(6);
pw.expect("gmpost").not.toHaveLength(5);
pw.expect(["apple", "banana", "coconut"]).toHaveLength(3);
pw.expect(["apple", "banana", "coconut"]).not.toHaveLength(4);
.toInclude(value)
使用 .toInclude(value) 检查字符串/数组是否包含目标值。
// These expectations will pass
pw.expect("gmpost").toInclude("gm");
pw.expect("gmpost").not.toInclude("post");
pw.expect(["apple", "banana", "coconut"]).toInclude("banana");
pw.expect(["apple", "banana", "coconut"]).not.toInclude("grape");
.toHaveProperty(attribute)
使用 .toHaveProperty(attribute) 检查 object/JSON 是否存在目标属性。
pw.expect(pw.response.body).toHaveProperty("data");
pw.expect(pw.response.body).not.toHaveProperty("errors");
.response
通过访问 pw.response 对象,获得响应数据。
// This test will pass
pw.test("Response is ok", () => {
pw.expect(pw.response.status).toBe(200);
pw.expect(pw.response.body).not.toHaveProperty("errors");
});
当前支持的响应值
status:-number-状态代码为整数。headers:-object-响应头。body:-object-响应中的数据。在许多请求中,这是服务器发送的JSON。
.env.set("variable","value")
为选定环境变量指定一个值。如果未设置环境变量,测试结果中将显示一个警报,以将变量添加到全局环境或创建新环境。
注意:如果变量已经设置,它将被覆盖。
pw.env.set("variable", "value");
pw.env.set("body", pw.response.body);
.env.get("variable")
检索选定环境变量的值。接受环境变量作为参数。
pw.env.get("variable");
pw.env.get("baseURL");
.env.getResolve("variable")
递归检索所选环境变量的值。接受环境变量作为参数。
pw.env.getResolve("variable");
pw.env.getResolve("baseURL");
.env.resolve("variable")
递归检索所选环境变量的值。接受环境变量字符串作为参数。
pw.env.resolve("<<variable_1>><<variable_2>>");
pw.env.resolve("<<baseURL>><<basePath>>");
示例
基本API测试的示例
Testing HTTP status code
让我们编写一个测试,检查对请求的响应的状态代码是否为 200 ,以及响应正文中是否没有错误。
我们将使用 URL https://www.httpbin.org/status/200 在这种情况下,我们需要编写两个expect语句,
一个用于检查状态,另一个用于查看响应正文。
测试状态代码有两种方法
-
检查是否是
200:pw.expect(pw.response.status)toBe(200) -
使用matcher函数快速方便地测试http状态代码,在本例中为
toBeLevel2xx():pw.expect(pw.response.status)toBeLevel2xx()
Test Script
pw.test("Response is ok", () => {
pw.expect(pw.response.status).toBe(200);
pw.expect(pw.response.body).not.toHaveProperty("errors");
});
Alternate
pw.test("Response is ok", () => {
pw.expect(pw.response.status).toBeLevel2xx();
pw.expect(pw.response.body).not.toHaveProperty("errors");
});
发送请求后,这些测试将成功通过。

Assert Response payload
将数据解析为JSON,并从响应正文中判断属性。
在这个示例中,我们测试用户id是否指向特定用户。
让我们使用以下 GET API 端点 https://reqres.in/api/users/10.
我们将使用 .toBe 来判断特定的值,.toBeType来判断特定数据类型,如下面的代码片段所示。
pw.test("", () => {
const user = pw.response.body;
pw.expect(user.first_name).toBe("Byron");
pw.expect(user.first_name).toBeType("string");
});