POSTMAN的断言方法

下面以一个退出登录接口,在POSTMAN进行接口测试

接口:

{{something_uat1}}/api-homething/loginOutThird

返回值:

{
    "code": "200",
    "message": "操作成功",
    "dataMap": true
}

Postman接口截图:

Postman的test断言:

具体的TEST 方法:

说明:红色部分是对test方法的说明;橙黄色区域是Postman内test方法的原样例;下面黑色部分的断言方法是根据退出登录接口编写的具体实操。

//1.检测返回的结果包含字段 Response body:Contains a string
/*
pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});

*/
pm.test("检测返回结果中包含字段", function () {
    pm.expect(pm.response.text()).to.include("操作成功");
});

扫描二维码关注公众号,回复: 9691851 查看本文章

//2.获取环境变量的值:pm.variables.get("variable_key");
var data=pm.environment.get("something_uat1");
tests['检测设置的环境变量值']=data==="http://shomething.uat1.rs.com";

//3.测试返回的结果包含code:200
/*
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});
*/

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

//4.测试响应低于规定时长
/*
pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});
*/

pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});
//5.检测返回结果字符串 
/*pm.test("Body is correct", function () {
    pm.response.to.have.body("response_body_string");
});*/

pm.test("检测返回的整段文字是否相等", function () {
    pm.response.to.have.body('{"code":"200","message":"操作成功","dataMap":true}');
});
//6.检测返回字段值与预期是否相等.下面有三种写法Response body:JSON value check
/*
pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(100);
});
*/

//1).postman方法将字符串转换成json对象
pm.test("测试返回的message的值", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.message).to.eql("操作成功");
});
//2).JSON语法将字符串转换成json对象
pm.test("测试返回的dataMap的值", function () {
    var jsonData =JSON.parse(responseBody);
    jsonData.dataMap===true;
});
//3)使用tests方法来进行断言
var jsondata =JSON.parse(responseBody);
tests["测试返回的code=200"]=jsondata.code==="200";
//7.返回code包含任意元素
/*
pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201,202]);
});*/

pm.test("返回code包含任意元素", function () {
    pm.expect(pm.response.code).to.be.oneOf([200,202]);
});

//8 校验JSON的文档格式 Use Tiny Validator for JSON data
/* 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;
});      */

//注意:type类型内的 string,boolean都要小写 且加引号 

//解释说明,下方定义一个schema描述JSON文档格式
var schema = {
    "properties":{
        "code": {
        "type": "string",
         "description":"return code"
                 },
        "message":{
        "type": "string",
        "description":"return message"
                },
        "dataMap": {
        "type": "boolean",
        "description":"return if it  is successful"
                }
        },
    "required": ["code", "message", "dataMap"]
};

//获取接口返回的JSON格式的数据

var data=JSON.parse(responseBody);

//校验返回的responseBody和定义的schema是否相符
pm.test('Schema is valid', function() {
  pm.expect(tv4.validate(data, schema)).to.be.true;
 
});

发布了99 篇原创文章 · 获赞 43 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/mayanyun2013/article/details/88697748
今日推荐