Postman接口返回结果验证

1.最基本的返回验证

//1.验证返回状态码是否是200
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});
//2.验证返回body内是否含有某个值
pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
//3.验证某个返回值是否是100
pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(100);
});
//验证返回body中是否含有某个字符串
pm.test("Body is correct", function () {
    pm.response.to.have.body("response_body_string");
});
//验证返回头类型
pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});
//验证请求时长是否小于200ms
pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});
//验证返回码是否为200
pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201,202]);
});
//验证返回数据中是否包含某个字符串
pm.test("Status code name has string", function () {
    pm.response.to.have.status("Created");
});
//验证json数据的微小验证器
var jsonObject = xml2Json(responseBody);
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;
});

*********************

完整实例001

使用Postman进行接口测试时,如何对接口响应的结果进行校验呢。判断接口返回的实际结果是否符合预期结果,需要使用到Postman的断言功能。一般情况下,断言一个接口的响应结果会根据响应状态码或响应结果中的关键字段的值进行判断。

断言:检验预期值与实际值是否相等或是否匹配
预期值 = 实际值 则测试通过OK
预期值 ≠ 实际值 则测试不通过Fail

Postman针对接口的请求报文和响应报文分别提供了Pre-request Script、Test Script,这两个是建立在JavaScript语言环境基础上的
Pre-request Script:预置脚本。可用来修改一些默认参数,在请求发送之前执行。
Test Script:后置测试脚本。当接收到响应之后,再执行测试脚本。

以下针对具体案例说明Postman的断言功能的使用

响应结果json字符串1
请求URL:http://{{IP}}:{{Port}}/web/restful/claim/pushClaim
请求方式:post
传递参数:accidentNo=随机数
断言规则
A.响应状态码:分别对不同状态码进行处理输出
B.响应内容:返回的accidentNo参数值与定义的一致
C.响应时间:小于0.8s

{
    "accidentNo": "Acc_560295",
    "auditReport": {
        "auditRuleTriggers": []
    },
    "claimUniqueId": "claim_131468",
    "interfaceCode": "ClaimPush",
    "message": "success",
    "resultCode": "000"
}

1、请求发送前,参数设置Pre-request Script

// 随机事故号
environment.accidentNo = `Acc_${randomInt(100000, 999999)}`;
pm.environment.set("accidentNo", environment.accidentNo);
2、请求响应后,在Test Script中进行判断操作,思路如下
1)、对预期结果和返回结果进行判断
2)、判断相等则设置tests语句为true
3)、判断不相等则设置tests语句为false,即失败,并且打印实际接口定义的响应代码
var state=responseCode.code;//获取返回状态
var number=(state.toString()).substr(0,1);//将返回的number类型转为string类型,并获取第一位
switch(number){
case '2':
    test();
    break;
case '4':
    clientQue(); //4开头的状态,简单定义为客户端问题
    break;
case '5':
    serverQue(); //5开头的状态,简单定义为服务器问题
    break;
default:
    tests['测试不通过,状态='+state]=false;  //如出现其他情况,则打印状态,并测试不通过
    break;
}
 
function test(){ //状态为200执行的函数
var accidentNo = pm.environment.get("accidentNo",accidentNo);

var result = JSON.parse(responseBody);
 if(result.accidentNo==accidentNo){
        tests["测试通过(事故号=========="+accidentNo+")"]=true;
    }else{
        tests["测试失败 (响应代码="+result.resultCode+")"]=false;
   }
}

//客户端问题
function clientQue(){
    tests['客户端问题(请求参数或方式错误)---测试失败---状态码为'+state+'   requestURl为'+request.url]=false;
}
 
//服务器或者网关问题
function serverQue(){
    tests['服务器或网关问题---测试失败---状态码为'+state+'   requestURl为'+request.url]=false;
}

//检验响应时间是否小于0.8s
pm.test("响应时间 < 800ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(800);
});
响应结果json字符串2
请求URL:http://{{IP}}:{{Port}}//web/rest/submitTask
请求方式:post
传递参数:accidentNo=随机数
断言规则
A.响应状态码:是否为200
B.响应内容:是否包含指定字符串
C.响应时间:小于0.8s
{
    "responseBodyVo": {
        "auditRuleTriggers": [
            {
                "actualValue": "",
                "auditScore": 10,
                "itemInfoList": [
                    {
                        "claimItemUniqueId": "Unique0005",
                        "feeAfterDiscount": 100,
                        "itemName": "发动机油",
                        "operationTypeId": "05",
                    }
                ],
                "itemName": "玻璃胶",
                "ruleName": "请核实是否属于保险责任",
                "ruleNo": "010201002X",
                "ruleType": "03",
            }
  ]
}

1、请求发送前,参数设置Pre-request Script

// 随机事故号
environment.accidentNo = `Acc_${randomInt(100000, 999999)}`;
pm.environment.set("accidentNo", environment.accidentNo);

2、请求响应后,在Test Script中进行判断操作,思路如下

//检验JSON格式的响应数据中ruleNo是否为"0102010020"
if(tests["code is 200"] = responseCode.code === 200){ 
    pm.test("返回ruleName值 = 请核实是否属于保险责任", function () {
        var jsonData = pm.response.json();
        if(pm.expect(pm.response.text()).to.include("0102010020")){
             pm.expect(pm.response.text()).to.include("请核实是否属于保险责任");
        }
       
    });
}else{
     pm.test("测试不通过",function(){});
}


//检验响应时间是否小于0.8s
pm.test("响应时间 < 800ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(800);
});

var registNo = pm.environment.get("registNo");
var deflossMainId = pm.environment.get("deflossMainId");
//在Postman Console控制台输出registNo、deflossMainId值
// console.log(registNo);
// console.log(deflossMainId);

//检验响应的状态码是否为200
pm.test("报案号"+registNo+"   定损单号"+registNo+"    Status code is 200", function () {
    pm.response.to.have.status(200);
});

//检验是否包含规则 "0102010020"
pm.test("包含字符串'0102010020'", function () {
    pm.expect(pm.response.text()).to.include("0102010020");
});
链接:https://www.jianshu.com/p/f115459077a1
原文地址:https://www.cnblogs.com/yimai-series/p/13221104.html