码迷,mamicode.com
首页 > 其他好文 > 详细

postman的Testing examples(测试脚本示例)

时间:2017-11-07 17:59:45      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:amp   ant   设置   请求   ida   res   获得   man   response   

测试代码会在发送request并且接收到responses后执行。

  • 1.设置环境变量 postman.setEnvironmentVariable("key", "value");
  • 2.设置全局变量 postman.setGlobalVariable("key", "value");
  • 3.检查response body中是否包含某个string tests["Body matches string"] =responseBody.has
    ("string_you_want_to_search");
  • 4.检测JSON中的某个值是否等于预期的值
    var data = JSON.parse(responseBody);tests["Your test name"] = data.value === 100;
    JSON.parse()方法,把json字符串转化为对象。parse()会进行json格式的检查是一个安全的函数。 如:检查json中某个数组元素的个数(这里检测programs的长度)
    var data = JSON.parse(responseBody);tests["program‘s lenght"] = data.programs.length === 5;
  • 5.转换XML body为JSON对象 var jsonObject = xml2Json(responseBody);
  • 6.检查response body是否与某个string相等 tests["Body is correct"] = responseBody === "response_body_string";
  • 7.测试response Headers中的某个元素是否存在(如:Content-Type)
    tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //getResponseHeader()方法会返回header的值,如果该值存在
    或者:
    tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");

     

    上面的方法,不区分大小写。下面的方法,要区分大小写。
    技术分享
     
  • 8.验证Status code的值 tests["Status code is 200"] = responseCode.code === 200;
  • 9.验证Response time是否小于某个值 tests["Response time is less than 200ms"] = responseTime < 200;
  • 10.name是否包含某个值 tests["Status code name has string"] = responseCode.name.has("Created");
  • 11.POST 请求的状态响应码是否是某个值 tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;
  • 12.很小的JSON数据验证器
    var schema = { "items": { "type": "boolean" }};var data1 = [true, false];var data2 = [true, 123];console.log(tv4.error);tests["Valid Data1"] = tv4.validate(data1, schema);tests["Valid Data2"] = tv4.validate(data2, schema);

     

    结果:
    技术分享


作者:古佛青灯度流年
链接:http://www.jianshu.com/p/35678284ce78
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

postman的Testing examples(测试脚本示例)

标签:amp   ant   设置   请求   ida   res   获得   man   response   

原文地址:http://www.cnblogs.com/jtestroad/p/7799378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!