ChatGPT与Postman协作完成接口测试(四)
如果读者对Postman比较熟悉,一定会发现接口测试用例还存在可以优化的地方,例如对响应结果的验证。在接口测试工作中,仅仅发送请求而不对响应结果进行验证,是不够严谨的。因此,我们可以引导ChatGPT加入响应状态码断言、响应文本断言等。这里笔者输入相应提示词让ChatGPT在Postman脚本中加入使用pm.test的断言语句,如图5-24所示。
图5-24 加入使用pm.test的断言语句的Postman测试用例脚本(部分)
加入使用pm.test的断言语句的Postman测试用例脚本如下所示,从中可以看到已经加入了状态码和文本验证的断言语句,即黑粗体字部分的内容。
加入使用pm.test的断言语句的Postman脚本
以下是经过修改的 Postman 测试用例脚本,包含使用‘pm.test()’方法的断言,结合注册的 ‘username’值,同时不使用环境变量:
{
"info": {
"_postman_id": "09e63d0d-9a62-4c3b-8a2d-76f352a12312",
"name": "用户注册接口测试",
"description": "测试用户注册接口的正常和异常情况",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "正常注册",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "username",
"value": "testuser",
"type": "text"
},
{
"key": "password",
"value": "testpassword",
"type": "text"
},
{
"key": "passwordConfirm",
"value": "testpassword",
"type": "text"
},
{
"key": "firstName",
"value": "John",
"type": "text"
},
{
"key": "lastName",
"value": "Doe",
"type": "text"
},
{
"key": "address1",
"value": "123 Main St",
"type": "text"
},
{
"key": "address2",
"value": "98765",
"type": "text"
},
{
"key": "register.x",
"value": "1.23",
"type": "text"
},
{
"key": "register.y",
"value": "4.56",
"type": "text"
}
]
},
"url": {
"raw": "http://127.0.0.1:1080/cgi-bin/login.pl",
"protocol": "http",
"host": [
"127",
"0",
"0",
"1"
],
"port": "1080",
"path": [
"cgi-bin",
"login.pl"
]
}
},
"response": [],
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
"});",
"pm.test(\"注册成功消息包含用户名\", function () {",
" pm.expect(pm.response.text()).to.include(\"Thank you, <b>testuser</b>\");",
"});"
],
"type": "text/javascript"
}
}
]
},
{
"name": "密码不匹配",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "username",
"value": "testuser",
"type": "text"
},
{
"key": "password",
"value": "testpassword",
"type": "text"
},
{
"key": "passwordConfirm",
"value": "mismatchedpassword",
"type": "text"
}
]
},
"url": {
"raw": "http://127.0.0.1:1080/cgi-bin/login.pl",
"protocol": "http",
"host": [
"127",
"0",
"0",
"1"
],
"port": "1080",
"path": [
"cgi-bin",
"login.pl"
]
}
},
"response": [],
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"Status code is 400\", function () {",
" pm.response.to.have.status(400);",
"});",
"pm.test(\"注册失败消息包含错误信息\", function () {",
" pm.expect(pm.response.text()).to.include(\"Registration failed. Please check your input and try again\");",
"});"
],
"type": "text/javascript"
}
}
]
}
]
}
这个脚本包括了使用 pm.test() 方法的断言,以验证正常注册和密码不匹配两种情况下的响应消息。希望这对您有所帮助!
至此,我们一起验证了ChatGPT生成的Postman测试用例脚本完全可以正常运行。同时,利用ChatGPT还可以在Postman测试用例脚本中加入断言语句等,以不断完善接口测试用例脚本,让测试结果的展现更一目了然。
原文地址:https://blog.csdn.net/tony2yy/article/details/144709724
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!