响应以及断言
在“发送HTTP申请”一讲中,咱们解说了APIPOST中响应数据的查看。
API 申请响应
点击发送按钮后,如果有数据返回,则会显示返回数据,响应工夫,响应码,Cookie等。
留神:返回数据默认是 ==丑化== 模式,便于查看 JSON XML 格局。您能够通过切换 ==原生== 或 ==预览== 模式 查看其它类型的类型。
返回Headers
除了查看后果外,ApiPost也提供了弱小的测试校验性能。在这里咱们也能够应用断言来进行响应后果的校验。
响应后果分屏展现
在APIPOST 5.4版本后,反对“响应后果分屏展现”,从而晋升工作区的空间。
什么是断言?
合作开发,版本升级,服务器降级,接口返回有可能因为一些bug,和咱们预期后果不统一。为了便于开发&测试人员可能更快的发现bug,有利于整个产品质量以及进度的保障。咱们推出断言性能。
如何应用断言?
- 定义测试用例
- 验证测试用例
例如接口返回:
{ "errcode": 0, "errstr": "success", "post": [], "get": [], "request": [], "put": "", "header": { "Host": "echo.apipost.cn", "Connection": "keep-alive", "Content-Length": "0", "Accept": "application/json, text/javascript, */*; q=0.01", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "zh-CN", "Content-Type": "application/json", "Cookie": "PHPSESSID=n3k73k06o6ghnie4e9re4rbf0t", "Origin": "https://echo.apipost.cn", "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1" }}
定义测试用例:
apt.assert('response.raw.status==200');apt.assert('response.raw.type=="json"');apt.assert('response.json.errcode==0');apt.assert('response.raw.responseTime<100');apt.assert('response.json.header.Host=="echo.apipost.cn"');
点击发送按钮后:
绿色示意测试通过,红色示意测试不通过。
特地留神:==每个测试用例是一行,不能换行。==
例:apt.assert('response.json.header.Host=="echo.apipost.cn"');
1)response.json.header.Host 示意响应json上面的header数组中的Host字段,
2)必须都为1,才会通过。
常见的测试用例能够通过后执行脚本获取:
更多响应参数变量
response.raw:原始响应数据
调用示例:
response.raw.status //响应状态码(200、301、404等)response.raw.responseTime //响应工夫(毫秒)response.raw.type //响应类型(json等)response.raw.responseText //响应文本
response.json:json格局的响应数据
调用示例如下面示例:
response.json.data.token //也能够 response.json.data["token"]
response.headers:响应头
调用示例:
response.headers.server //也能够 response.headers["server"]
response.cookies :响应cookie
调用示例:
response.cookies.PHPSESSION //也能够 response.cookies["PHPSESSION"]
罕用断言表达式
1、查看response body中是否蕴含某个string
apt.assert('response.raw.responseText=="test"'); // 查看响应文本是否等于test字符串 apt.assert('response.raw.responseText.indexOf("test") > -1'); // 查看响应文本是否含有test字符串
2、检测返回JSON中的某个值是否等于预期的值
apt.assert('response.json.hasOwnProperty("errcode")'); // 检测返回json对象的是否含有errcode字段apt.assert('response.json.errcode=="success"'); // 检测返回json对象的errcode字段是否等于success字符串apt.assert('response.json.errcode.indexOf("success") > -1'); // 检测返回json对象的errcode字段是否含有success字符串apt.assert('response.json.errcode!="success"'); // 检测返回json对象的errcode字段是否不等于success字符串apt.assert('response.json.errcode>=1'); // 检测返回json对象的errcode字段是否大于1apt.assert('response.json.errcode==null'); // 检测返回json对象的errcode字段是否是null
3、测试response Headers中的某个元素是否存在(如:Content-Type)
apt.assert('response.headers.hasOwnProperty("content-type")');
4、验证Status code(响应码)的值是不是等于200
apt.assert('response.raw.status==200');
5、验证Response time(申请耗时)是否大于某个值
apt.assert('response.raw.responseTime>=100');
6、验证返回类型是不是json
apt.assert('response.raw.type=="json"');