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://blog.csdn.net/qq_26808915/article/details/50440278http://www.jianshu.com/p/615f668d91a6
- Postman官网
Postman - Newman
newman - npm - Postman Collection
Grouping requests in collections | Postman Learning Center - Postman Environment
Managing environments | Postman Learning Center - How to write powerful automated API tests with Postman, Newman and Jenkins
How to write powerful automated API tests with Postman, Newman and Jenkins | Postman Blog