zoukankan      html  css  js  c++  java
  • Postman的使用

    一、断言

    tests["响应小于1000ms"] = responseTime < 1000;
    tests["包含内容"] = responseBody.has("老佛爷的一生");
    tests["响应码200"] = responseCode.code===200;
    tests["响应码200或者202"] = responseCode.code === 200 || responseCode.code === 202;
    
    //解析responseBody
    var responseJSON=JSON.parse(responseBody);
    tests['断言'] = (_.get(responseJSON,'news[0].id')===9707867);
    //只能判断1级
    tests["只能断言第一层"] = responseJSON["date"]==='20190220';
    
    //获取下一级数据
    var builds= responseJSON['news'];
    //在控制台打印 (View —— Show Postman Console)
    console.log(" builds: ", builds);
    tests['获取下一级'] = builds[0]['id']===9707867;
    
    //getResponseHeader()方法会返回header的值,如果该值存在
    tests["获取响应头"] = postman.getResponseHeader("Content-Type");

    二、全局变量、局部变量以及变量的传递

    var responseJSON=JSON.parse(responseBody)
    //拿到响应内容的数据,然后设置为局部或全局变量
    var A=responseJSON['token']
    var B=_.get(responseJSON,'token')
    //设置全球变量,可以人工添加
    postman.setGlobalVariable("全局变量", A);
    //设置局部变量,可以人工添加
    postman.setEnvironmentVariable("局部变量", "http://news-at.zhihu.com");

     三、验证JSON的语法

    //Postman测试沙箱是一个JavaScript执行环境,可以通过JS脚本来编写pre-requist和测试脚本。pre-requist可以用来修改一些默认参数。
    //Postman沙箱集成了几个工具库,比如lodash、SugarJs、tv4,还有一些内置函数如xml2JSON.
    //tv4用于验证JSON数据,通过编写JSON Schema来验证,JSON Schema的语法请参照这里

    var schema1 = {
     "items": {
     "type": "boolean",
     }
    };
    var data1 = [true, false];
    var data2 = [true, 123];
    
    tests["Valid Data1"] = tv4.validate(data1, schema1);
    tests["Valid Data2"] = tv4.validate(data2, schema1);
    //打印报错信息
    console.log("Validation failed: ", tv4.error);
    
    var schema2 = {
      properties: {
          status: {type: 'string'},
          last_updated: {type: 'string'}
      }
    };
    
    tests["Valid data schema"] = tv4.validate(responseBody, schema2);

     四、判断解析的是否为有效JSON

    //解析打印,并判断是否为有效的json
    try{
        var t=JSON.parse(responseBody);console.log('解析内容',t)
        tests['是有效json']=true
    }
    catch(e){
        tests['不是有效json']=false
    }

     五、运行测试集合

    测试进阶轨迹
  • 相关阅读:
    CLR via C#深解笔记三
    CLR via C#深解笔记二
    CLR via C#深解笔记一
    C#参考:Linq 概述
    JavaScript
    jQuery
    JavaScript
    云原生
    python模块----optparse模块、argparse模块 (命令行解析模块)
    python模块----pymysql模块 (连接MySQL数据库)
  • 原文地址:https://www.cnblogs.com/yinwenbin/p/10409109.html
Copyright © 2011-2022 走看看