Postman API测试工具 - 断言 基本使用(二)

PostMan工具断言

断言:诊断语言,检查点。检查返回的结果是否是正确。

怎么用利用Postman工具添加断言

在这里插入图片描述

常见的验证:

验证返回码必须是200

在这里插入图片描述

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

响应时间验证:

在这里插入图片描述

pm.test("Response time is less than 200ms", function () {
    
    
    pm.expect(pm.response.responseTime).to.be.below(200);
});

请求成功验证

在这里插入图片描述

pm.**test**("Successful POST request", **function** () {
    
    

  pm.expect(pm.response.code).to.be.oneOf([201,200202]);

});

包含某个字符串验证
在这里插入图片描述

pm.test("Body matches string", function () {
    
    
    pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});

json某个值检查:

在这里插入图片描述


pm.test("Your test name", function () {
    
    
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(100);
});
pm.test("验证返回码是200,返回是成功的",function(){
    
    

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

  //检查返回的状态是200

})
pm.test("验证状态码返回是200", function () {
    
    
    pm.response.to.have.status(200);
    //验证返回码必须是200
});

pm.test("Response time is less than 200ms", function () {
    
    
    pm.expect(pm.response.responseTime).to.be.below(15);
    //验证处理的时间不超过200ms
});

// 设置一个变量,把返回的Json数据存入变量中(jsonData)
var jsonData = pm.response.json();



pm.test("message城市的结果包含了长沙市" ,function(){
    
    
    pm.expect(jsonData.forecasts[0].city).to.eql("长沙市")
})

//验证json数据中某一个字符串的值 
pm.test("message的结果是查询成功", function () {
    
    
pm.expect(jsonData.message).to.eql("查询成功")
});

//josn格式多节点值的验证
pm.test("验证查询出的品牌ID的正确", function () {
    
    
    var jsonData = pm.response.json();
    pm.expect(jsonData.data.id).to.eql(6742);
});

//josn格式多节点多个返回的组值,使用角标(索引)进行测试
pm.test("验证查询出的品牌ID的正确", function () {
    
    
    var jsonData = pm.response.json();
    pm.expect(jsonData.data.rows[4].id).to.eql(2222);
});

//josn格式多节点多个返回的组值,使用角标(索引)进行测试
pm.test("验证查询出的品牌图片的正确性", function () {
    
    
    var jsonData = pm.response.json();
    pm.expect(jsonData.data.rows[2].image).to.eql("http://img10.360buyimg.com/popshop/jfs/t5662/36/8888655583/7806/1c629c01/598033b4Nd6055897.jpg");
});

// 验证返回结果中包含某一个字符串
pm.test("Body matches string", function () {
    
    
    pm.expect(pm.response.text()).to.include("华为");
});


// 检查接口返回的性能问题,不超过300ms, 如果超过1000ms
pm.test("响应时间小于300ms,性能超牛逼", function () {
    
    
    pm.expect(pm.response.responseTime).to.be.below(500);
});

//检查返回码是200
pm.test("Status code is 200", function () {
    
    
    pm.response.to.have.status(200);
});

//检查接口调用返回成功
pm.test("Successful POST request", function () {
    
    
    pm.expect(pm.response.code).to.be.oneOf([201,200, 202]);
});

//检查结果中包含字符串
pm.test("包含小雨", function () {
    
    
    pm.expect(pm.response.text()).to.include("小雨");
});

//精细化检查,检查某一个键的值 

//设置一个全局变量,把响应的json的内容存储到变量jsonData中
var jsonData = pm.response.json();

//验证返回状态码是1,
pm.test("返回码为1", function () {
    
    
        pm.expect(jsonData.status).to.eql("1");
});

// 验证返回的值确定是长沙市 ,,如果数据的下一级,就使用“.”,如果是数组类型,就使用"[]."
pm.test("返回确定为长沙市", function () {
    
    
        pm.expect(jsonData.forecasts[0].city).to.eql("长沙市");
});


自动执行

选择需要自动执行的项目,点击 Run

在这里插入图片描述

选中需要执行的用例 :

单击run 运行

在这里插入图片描述

查看执行结果

在这里插入图片描述

导出测试结果

导出测试报告:

猜你喜欢

转载自blog.csdn.net/EXIxiaozhou/article/details/128217198