Postman API Testing 1

1.Import API文件

2. 分享及下载或导出

3.设置环境变量

{{Host}} Manage Evenmentions-Add Evenments:PROD/UAT/DEV/QA, Key:Host,Value:-,通过切换不同的环境来切换对应的Host.

4. 用{{}}来引用环境变量或者设置的全局或局部变量的值,可以传入header或者body的变量的value中。

5.在Collection的第一个API的Tests中设置一些全局变量供其他API调用。

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

//测试API返回是否OK

var responseJSON = JSON.parse(responseBody); 

//解析一个JSON字符串,构造由字符串描述的JavaScript值或对象

postman.setGlobalVariable("access_token", responseJSON.access_token);

//把这个API的responseBody里面的access_token设置为全局变量


postman.setGlobalVariable("refresh_token", responseJSON.refresh_token);


var Year = (new Date()).getFullYear();
var Month = (new Date()).getMonth();//0-11
Month = Month+1;//月份比现在少1


if(Month===1){//比如201701的上一月份为201612
var MonthNew = 12;//新月份为上一月份


var YearNew = Year-1;//新年份为去年


}else{
    MonthNew=Month-1;//新月份为上一月份
    YearNew = Year;//新年份为去年
}
 if (Month >= 1 && Month <= 9) {
        Month = "0" + Month;//1-9月前加0
    }
 if (MonthNew >= 1 && MonthNew <= 9) {
        MonthNew = "0" + MonthNew;//1-9月前加0
    }
var ReportMonth = Year+Month;


tests["This Year is"+Year] = responseCode.code === 200;
tests["This Month is"+Month] = responseCode.code === 200;



postman.setGlobalVariable("MonthNew",MonthNew);
postman.setGlobalVariable("YearNew",YearNew);

postman.setGlobalVariable("ReportMonth",ReportMonth);

6.验证返回数组schema

/*test points: 
1. Status code is 200
2. Response time is less than 5000ms
3. Response Values is not null
5. Valid responsedata Keys and Values schema


*/


tests["Status code is 200"] = responseCode.code === 200;
tests["Response time is less than 5000ms"] = responseTime < 5000;
 var jsonData = JSON.parse(responseBody);
tests["Response Value is not null"] = jsonData.ResponseData.Values[0] !== undefined;//如果第一组有值

//responseBody返回是数组,数组里面的第一组[]值不为空
var schema11 = {
 "items": [
            {
              "type": "string",
              "enum": [
                null,
                "Name"
              ]
            },
            {
              "type": "string",
              "enum": [
                null,
                "ID"
              ]
            }, 
          ]
}; //复制responseBody到Jsonschema.net,设置后生成相应的schema.
tests["Valid Keys schema"] = tv4.validate(jsonData.ResponseData.Keys, schema11);//验证responseBody的schema
console.log("Validation failed: ", tv4.error);


/*var schema12 = {"items": [
        {
          "type": "string"
        },
        {
          "type": "string"
        },
        {
          "type": "string"
        },
        {
          "type": "string"
        },
        {
          "type": "string"
        },
        {
          "type": "integer"
        },
        {
          "type": "integer"
        },
        {
          "type": "number"
        }
      ]
};//二维数组的第一组数组的schema(如果第一组有值)
tests["Valid Values schema"] = tv4.validate(jsonData.ResponseData.Values[0], schema12);
console.log("Validation failed: ", tv4.error);
*/

7. 单个API测试通过,run整个collection

选择collection 文件夹,测试环境,执行多少次Interation,请求的延迟Delay,以及传入的参数的数据文件json文件或csv文件,点击start run,可以看到results

8. Newman 命令行run,快速把Postman与CI集成起来
在collection的json文件目录下执行

newman -c Test.postman_collection.json -e PR

OD.postman_environment.json -H report.html

参考:

1. API自动化测试利器——Postman

http://www.tuicool.com/articles/FJNvQrB
2. Jason schema语法
http://json-schema.org/example1.html
3. Jason schema https://segmentfault.com/a/1190000005055899
4. API测试 http://www.jianshu.com/p/ca0527020a3f
5.  web接口测试中需要测试的几个点
http://www.studyofnet.com/news/1164.html
6.  API与接口 https://zhidao.baidu.com/question/371363128986343284.html
7.  

说说接口测试那些事

http://blog.csdn.net/qq_26808915/article/details/50440278
8.  API开发神器-Postman
http://www.jianshu.com/p/615f668d91a6

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值