在Ubuntu上使用Postman进行API测试的“Tests”功能,可以按照以下步骤操作:
pm.response.to.have.status(200)
来检查状态码是否为200。以下是一个简单的示例脚本,用于检查响应状态码是否为200,并在控制台输出调试信息:
// Pre-request Script
console.log("Pre-request script running");
// 访问环境变量
const token = pm.environment.get("token");
// 设置请求头
pm.request.headers.add({ key: "Authorization", value: "Bearer " + token });
// Tests
pm.test("Response status code is 200", function () {
pm.response.to.have.status(200);
});
// 检查响应时间
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
my_api_tests.json
)。newman run path/to/your/my_api_tests.json
请将 path/to/your/my_api_tests.json
替换为你的Postman集合文件的实际路径。
通过以上步骤,你可以在Ubuntu系统中使用Postman进行API自动化测试,提高测试效率和准确性。