postman 进阶技巧

  1. cookie
    清除缓存

  2. code
    生成接口自动化测试脚本

  3. 响应部分

    1. pretty 响应以json或xml显示
    2. raw 响应以文本显示
    3. preview 以HTML网页行驶显示
  4. 断言
    断言:用于判断接口请求是否成功
    最少2个:
    - 状态断言:200
    //状态断言:
    //断言状态码为200
    pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
    });

    - 业务断言:可以有多个

//断言返回的结果中包含一个字符串
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("access_token");
});
//检查返回的JOSN数据的值。
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.expires_in).to.eql(7200);
});
//断言返回的结果等于一个字符串
pm.test("Body is correct", function () {
pm.response.to.have.body("{"errcode":0,"errmsg":"ok"}");
});
//断言响应头中是否包含Content-Type
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Connection");
});
//断言接口的请求时间少于200ms
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});

  1. 环境变量
  2. 全局变量
  3. 获取响应信息的值
    • 获取json值
      var jsDate = JSON.parse(responseBody);
      console.log(jsDate.data.tokenId);
      pm.globals.set("new_token",jsDate.data.tokenId);
      • 通过正则表达式提取 (.+?)
        var access_token = responseBody.match(new RegExp('"tokenId":"(.+?)"'));
        pm.globals.set("new_token",access_token);
        console.log(access_token);
        8.内置参数
        {{timestamp}} 服务器的时间戳 {{randomInt}} 随机数0-1000的随机数
        {{$guid}} 随机字符串
        9.非GUI执行
  • 导出接口用例

    • export 接口用例
    • 导出环境变量 json
    • 导出全局变量 json
  • newman 在Linux运行
    newman:专为postman而生,执行非GUI方式
    命令:
    newman run "e:yongli.json" -e "e:huanjing.json" -g "e:quanju.json" -r cli,html,json,junit --reporter-html-export "e: esult.html"

      •        -e 前面的参数是接口用例的位置
      • -g 前面的参数是环境变量的位置
      • -r前面的参数是全局变量的位置
      • --reporter-html-export  后面的参数是生成的测试报告的位置
  • 集成Jenkins

原文地址:https://www.cnblogs.com/xiaoyujade/p/13268276.html