3.Postman中的接口Tests断言执行&常用Tests用法

1. 设置断言检测一些常见数据

//校验响应状态码

pm.test("Status is 200", function () {

    pm.response.to.have.status(200);

});

// 断言状态码200-202区间

pm.test("Successful GET request", function () {

   pm.expect(pm.response.code).to.be.oneOf([200,202]);

});

//校验接口返回状态

pm.test("接口返回状态status ", function () {

    var jsonData = pm.response.json();

    var status1 =jsonData.status

    pm.expect(status1).to.eql(0);

    console.log("status1 is :" +status1);

});

//断言响应时间小于200ms

pm.test("Response time is less than 300ms", function () {

   pm.expect(pm.response.responseTime).to.be.below(3000);

});

//断言响应中包含某个字符串

pm.test("Body matches string", function () {

   pm.expect(pm.response.text()).to.include("FRM");

});

//断言响应中的字段等于某个值

pm.test("message test", function () {

   var jsonData = pm.response.json();

   pm.expect(jsonData["message"]).to.eql("请求成功");

});

//断言响应中的字段不等于某个值

var jsonData = JSON.parse(responseBody);

tests["message不为failed"] = jsonData["message"] != "failed";

// // 断言响应中"list"的字段长度

// pm.test("data list test", function () {

//    var jsonData = pm.response.json();

//    pm.expect(jsonData["data"].length).to.eql(0);

// });

// 断言响应中"list 0"time字段的值

pm.test("data list 0 test", function () {

   var jsonData = pm.response.json();

   pm.expect(jsonData["data"][0]["time"]).to.eql("2018-11-28 17:27:41");

});

//测试解析json数据

pm.test("Your test name", function () {

    var jsonData = pm.response.json();

    pm.expect(jsonData.value).to.eql(100);

 

});

//检查header

pm.test("Content-Type is present", function () {

    pm.response.to.have.header("Content-Type");

});

//测试是否等于某个字段

pm.test("Body is correct", function () {

    pm.response.to.have.body("response_body_string");

});

//请求时间校验

pm.test("Response time is less than 200ms", function () {

    pm.expect(pm.response.responseTime).to.be.below(200);

})

//返回值在某个区间

pm.test("Status code name has string", function () {

    pm.response.to.have.status("Created");

})

//使用json schema 校验结果

var schema = {

  "items": {

    "type": "boolean"

  }

};

var data1 = [true, false];

var data2 = [true, 123];

pm.test('Schema is valid', function() {

  pm.expect(tv4.validate(data1, schema)).to.be.true;

  pm.expect(tv4.validate(data2, schema)).to.be.true;

});

2. 常用Tests用法

 

1.检查response body中是否包含某个string

 

tests["Body matches string"] = responseBody.has("string_you_want_to_search");

 

注意:"Body matches string" 需唯一。

 

2.检测JSON中的某个值是否等于预期的值

 

var data = JSON.parse(responseBody);

 

tests["Your test name"] = data.value === 100;

 

JSON.parse()方法,把json字符串转化为对象。parse()会进行json格式的检查是一个安全的函数。

 

如:检查json中某个数组元素的个数(这里检测programs的长度)

 

var data = JSON.parse(responseBody);

 

tests["program's lenght"] = data.programs.length === 5;

 

3.检查response body是否与某个string相等

 

4.转换XML bodyJSON对象

 

var jsonObject = xml2Json(responseBody);

 

tests["Body is correct"] = responseBody === "response_body_string";

 

5.测试response Headers中的某个元素是否存在(:Content-Type)

 

tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");

 

//getResponseHeader()方法会返回header的值,如果该值存在

 

或者:

 

tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");

 

上面的方法,不区分大小写。下面的方法,要区分大小写。

 

6.验证Status code的值

 

tests["Status code is 200"] = responseCode.code === 200;

 

7.验证Response time是否小于某个值

 

tests["Response time is less than 200ms"] = responseTime < 200;

 

8.name是否包含某个值

 

tests["Status code name has string"] = responseCode.name.has("Created");

 

9.POST 请求的状态响应码是否是某个值

 

tests["Successful POST request"] = response

 

 

2-若执行接口时 Tests报错或 Test Results 栏报错,则设置变量未生效,定义需要核查

 

 

3-获取响应的数据中 msg 的值(jsonData.msg),然后赋值给字符“msg”
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("msg", jsonData.msg);
获取响应的数据中 msg 的值(jsonData.msg),然后赋值给字符“msg”
: 响应数据是 "msg": "Custom error message notification"

 

 

在获取关联参数的时候可以设置一个environment,把获取的值放进去,给下个请求调用。

如果没有设置environment,则获取的参数不会被保存。

注意点:这里的脚本要看响应的返回形式来修改,如:

var jsonData = JSON.parse(responseBody);

postman.setEnvironmentVariable("uid", jsonData.result.uid);   

这里响应返回的格式里面是 result数组下的uid,所以获取uid脚本是 jsonData.result.uid

 

 

posted @ 2020-12-14 20:38  星期~  阅读(915)  评论(0编辑  收藏  举报