• 接口自动化测试框架postman tests常用方法


    postman常用方法集合:

    1.设置环境变量

    1. postman.setEnvironmentVariable("key", "value");

    2. pm.environment.set("key", "value");//postman 5.0以上版本设置环境变量的方法

    2.设置全局变量

    1. postman.setGlobalVariable("key", "value");
    2. pm.globals.set("variable_key", "variable_value");//postman 5.0以上版本设置全局变量方法

    3.检查response body中是否包含某个string 

    1. tests["Body matches string"] = responseBody.has("string_you_want_to_search");
    2. pm.test("Body is correct", function () {
    3. pm.response.to.have.body("response_body_string");
    4. });//5.0以上版本方法

    4.检测JSON中的某个值是否等于预期的值

    1. var data = JSON.parse(responseBody);

    2. tests["Your test name"] = data.value === 100;

    JSON.parse()方法,把json字符串转化为对象。parse()会进行json格式的检查是一个安全的函数。

    如:检查json中某个数组元素的个数(这里检测programs的长度)

    1. var data = JSON.parse(responseBody);
    2. tests["program's lenght"] = data.programs.length === 5;

    5.转换XML body为JSON对象

    1. var jsonObject = xml2Json(responseBody);

    2. tests["Body is correct"] = responseBody === "response_body_string";

    6.检查response body是否与某个string相等

    7.测试response Headers中的某个元素是否存在(如:Content-Type)

    1. //getResponseHeader()方法会返回header的值,如果该值存在
    2. tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");
    3. tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");

    上面的方法,不区分大小写。下面的方法,要区分大小写。

    8.验证Status code的值

     

    1. tests["Status code is 200"] = responseCode.code === 200;
    2. pm.test("Status code is 200", function () {
    3. pm.response.to.have.status(200);
    4. });//5.0以上版本方法

     9.验证Response time是否小于某个值

    ​​​​​

    1. tests["Response time is less than 200ms"] = responseTime < 200;
    2. //5.0以上版本方法
    3. pm.test("Response time is less than 200ms", function () {
    4. pm.expect(pm.response.responseTime).to.be.below(200);
    5. });

    10.name是否包含某个值

    1. tests["Status code name has string"] = responseCode.name.has("Created");
    2. //5.0以上版本方法
    3. pm.test("Status code name has string", function () {
    4. pm.response.to.have.status("Created");
    5. });

    11.POST 请求的状态响应码是否是某个值

    1. tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;
    2. //5.0以上版本方法
    3. pm.test("Successful POST request", function () {
    4. pm.expect(pm.response.code).to.be.oneOf([201,202]);
    5. });

    12.很小的JSON数据验证器

    1. var schema = {
    2. "items": {
    3. "type": "boolean"
    4. }
    5. };
    6. var data1 = [true, false];
    7. var data2 = [true, 123];
    8. console.log(tv4.error);
    9. tests["Valid Data1"] = tv4.validate(data1, schema);
    10. tests["Valid Data2"] = tv4.validate(data2, schema);

    13.获取request.data值:

    var Json = JSON.parse(request.data); 
    • data {object}:
      this is a dictionary of form data for the request. (request.data["key"]=="value")
    • headers {object}:
      this is a dictionary of headers for the request (request.headers["key"]=="value")
    • method {string}:
      GET/POST/PUT etc.
    • url {string}:
      the url for the request.

     假设requestBody中有"version":"1.0";这个值,如果想获取到version的value值,代码如下

    1. var Json = JSON.parse(request.data);
    2. var version = Json["version"];

     14.JSON.parse()和JSON.stringify()

    1. JSON.parse()【从一个字符串中解析出json对象】
    2. JSON.stringify()【从一个对象中解析出字符串】
    3. var data={name:'goatling'}
    4. JSON.parse(data)
    5. 结果是: '{"name":"goatling"}'
    6. JSON.stringify(data)
    7. 结果是:name:"goatling"

     

    15.判断字段值是否为空typeof()

    1. var Jsondata = JSON.parse(responseBody);

    2. if( typeof(Jsondata.data) != "undefined" )

    感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

     

    这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取 

  • 相关阅读:
    vscode搭建LVGL开发环境
    【源码+文档+调试讲解】微信小程序家政项目小程序
    oled清屏函数记录
    生态系统服务—土壤侵蚀强度空间分布/降雨侵蚀力
    一篇教程搞定Windows系统中的Docker应用安装
    Siddhi cep
    金仓数据库KingbaseES客户端应用参考手册--11. sys_dump
    5.1 Ajax数据爬取之初介绍
    激流勇进小游戏
    个人前端编程技巧总结
  • 原文地址:https://blog.csdn.net/hlsxjh/article/details/134092288