postman断言的使用
判断状态码
pm.test("状态码是200", function () {
pm.response.to.have.status(200);
});
判断json指定key等于指定值
pm.test("msg等于账户与密码不匹配", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.msg).to.eql("账户与密码不匹配");
});
判断success指定true等于指定值
pm.test("success等于true", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.success).to.eql(true);
});
判断返回[]
pm.test("内容长度为0", function () {
var jsonData = pm.response.json();
//实际就是判断json长度等于0
pm.expect(jsonData.length).to.eql(0);
});
判断json数组条数大于指定值
pm.test("条数大于0", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.length).to.above(0);
});
判断json数组条数小于指定值
pm.test("条数小于5", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.length).to.above(5);
});
判断没有返回内容
pm.test("数据为空", function () {
pm.expect(pm.response.text()).to.eql("");
});
判断json的数据值是否等于指定值
第一种
var jsonData = JSON.parse(responseBody);
tests["Check respose status value"] = jsonData.status === 1;
第二种
pm.test("判断data里面第一个json数据的id为1", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.data[0].id).to.eql(1);});
Check if response body contains a string (检查响应主体是否包含字符串)
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
Check if response body is equal to a string (检查响应主体是否等于一个字符串)
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});
Check for a JSON value (检查JSON值)
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
Content-Type is present (内容类型存在)
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});
Response time is less than 200ms (响应时间小于200ms)
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
Status code is 200 (状态码是200)
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
Code name contains a string (代码名称包含一个字符串)
pm.test("Status code name has string", function () {
pm.response.to.have.status("Created");
});
Successful POST request status code (成功的POST请求状态码)
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201,202]);
});
Decode base64 encoded data (解码base64编码的数据)
var intermediate,
base64Content, // assume this has a base64 encoded value
rawContent = base64Content.slice('data:application/octet-stream;base64,'.length);
intermediate = CryptoJS.enc.Base64.parse(base64content); // CryptoJS is an inbuilt object, documented here: https://www.npmjs.com/package/crypto-js
pm.test('Contents are valid', function() {
pm.expect(CryptoJS.enc.Utf8.stringify(intermediate)).to.be.true; // a check for non-emptiness
});
Send an asynchronous request (发送异步请求)
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
console.log(response.json());
});
Convert XML body to a JSON object (将XML正文转换为JSON对象)
var jsonObject = xml2Json(responseBody);
判断请求body的值
pm.test("判断请求body是否包含name,images,detail,providerPrice,categoryId", function () {
var jsonData = pm.response.json();
// 一、变量Body:存储raw中,请求Body的参数
var jodabody = pm.request.body.raw;
// 二、将Body转化为Json格式的数据,使用 JSON.parse()
// var body_json = JSON.parse(jodabody);
//三、提取请求Body中的 id 参数值
pm.expect(jodabody).to.include("name");
pm.expect(jodabody).to.include("images");
pm.expect(jodabody).to.include("detail");
pm.expect(jodabody).to.include("providerPrice");
pm.expect(jodabody).to.include("categoryId");
});
设置全局变量
// 将获取到到token写环境变量中,供注册、登录、查询用户信息及退出请求使用
var JsonData = pm.response.json().data
pm.globals.set("logo_token",JsonData.token)
// 将获取到到id写环境变量中,供注册、登录、查询用户信息及退出请求使用
var JsonData = pm.response.json()
var len = JsonData.data.items.length;
var last = JsonData.data.items[len-1]
pm.globals.set("yingxiao_id",last.id)
利用tests进行断言
/*
// 推荐用全等 ===,确保类型和值都一致
tests['Status code is 200'] = responseCode.code === 200;
// 判断是否存在 'success' 值
tests["Body matches code"] = responseBody.has("0");
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("sessionId",jsonData.result);
tests[`[INFO] Request params: ${JSON.stringify(request.data)}`] = true;
tests["have result "]=jsonData.hasOwnProperty("error")!==true;
tests[`[INFO] Response timeout: ${responseTime}`] = responseTime < 6000;
**/
//状态代码是200
if(responseCode.code === 200){
// 判断是否存在 'success' 值,检查响应体包含一个字符串
tests["Body matches code"] = responseBody.has("0");
//响应结果中result保存为全局变量sessonId
var jsonData = JSON.parse(responseBody);
postman.setGlobalVariable("sessionId",jsonData.result);
//输入接口参数信息
tests[`[INFO] Request params: ${JSON.stringify(request.data)}`] = true;
// tests["have result "]=jsonData.hasOwnProperty("error")!==true;
//判断接口响应结果有result
tests["have result "]=jsonData.hasOwnProperty("result")===true;
//判断接口响应时间小于N秒
tests[`[INFO] Response timeout: ${responseTime}`] = responseTime < 6000;
}else{
//接口请求失败
tests["Waring:Request Failed. Please Fix!"] = false;
}
时间转成时间戳进行比较大小
pm.test("判断第三个商品时间是否大于第二个商品时间,比较时间戳", function () {
var jsonData = pm.response.json();
var firstTime = jsonData.data.items[1].createdAt
var threeTime = jsonData.data.items[2].createdAt
thisTime1 = firstTime.replace(/-/g,'/')
var time1 = new Date(thisTime1)
time11 = time1.getTime()
thisTime2 = threeTime.replace(/-/g,'/')
var time2 = new Date(thisTime2)
time22 = time2.getTime()
pm.expect(time11).to.above(time22)
});
|