前言
postman断言是JavaScript语言编写的,在postman客户端指定区域编写即可。
断言会在请求返回之后,运行,并根据断言的pass\fail情况体现在最终测试结果中。
一、添加断言
1、打开保存的postman完成的访问某保单退保接口请求。点击Send,查看响应信息。
(1).断言信息包括:保险产品名称、响应状态码、响应报文格式
a.响应状态码断言
右侧沙箱选择:Status code:Code is 200
b.响应JSON报文信息断言(保险名称断言)
补充:可以通过postman,console.log()调试
c.响应报文格式校验
<1>响应报文需要转义处理(可以借助相关编辑器https://www.json.cn/json/jsononline.html):
<2>响应报文转义处理时,选择原始报文方式(raw)(否则存在空格想象,导致断言失败)
d.通过读取csv文件数据断言
<1>准备csv测试数据
<2>调试脚本(调用csv文件数据:data.csv文件字段名称)
<3>选择csv文件并执行脚本及查看结果
e.正则表达式:包含正则表达式模式和标志的变量名或字符串文字。将字符串与正则表达式匹配,并返回包含该搜索结果的数组。
正则表达式格式:match(‘开始标志位字符(.*?)结束标志位字符’)
<1>正则表达式(match):正则表达式中“.”标识匹配一个字符;“”匹配第1个和最后一个之间所有数据;“+”标识匹配前面的子表达式1次货多次 “ ?”为懒惰字符,首次遇到右边界会停止匹配:如:var6=responseBody.match('**“**riskName”:"(.?)"'),
<2>通过正则表达式设置断言
f.集合中添加通用断言设置
g.断言语法 tests[‘case’]=***
二、断言内容详解
断言判断可以通过环境变量和正则表达式实现:
1.设置环境变量–Setting an environment variable
postman.setEnvironmentVariable(“key”, “value”);
2.设置全局变量–Set a global variable
postman.setGlobalVariable(“key”, “value”);
3.检查响应中包含string–Check if response body contains a string
tests[“Body matches string”] = responseBody.has(“string_you_want_to_search”);
4.转化XML格式的响应成JSON对象—Convert XML body to a JSON object
var jsonObject = xml2Json(responseBody);
5.检查响应body中等于指定string–Check if response body is equal to a string
tests[“Body is correct”] = responseBody === “response_body_string”;
6.检查JSON某字段值–Check for a JSON value
var data = JSON.parse(responseBody);
tests[“Your test name”] = data.value === 100;
7.检查Content-Type是否包含在header返回(大小写不敏感)–Content-Type is present (Case-insensitive checking)
tests[“Content-Type is present”] = postman.getResponseHeader(“Content-Type”); //Note: the getResponseHeader() method returns the header value, if it exists.
8.检查Content-Type是否包含在header返回(大小写敏感)–Content-Type is present (Case-sensitive)
tests[“Content-Type is present”] = responseHeaders.hasOwnProperty(“Content-Type”);
9.检查请求耗时时间小于200ms–Response time is less than 200ms
tests[“Response time is less than 200ms”] = responseTime < 200;
10.检查Status code为200–Status code is 200
tests[“Status code is 200”] = responseCode.code === 200;
11.检查Code name是否指定string–Code name contains a string
tests[“Status code name has string”] = responseCode.name.has(“Created”);
12.检查成功post的请求status code–Succesful POST request status code
tests[“Successful POST request”] = responseCode.code === 201 || responseCode.code === 202;
本文转载:o小兵o