zoukankan      html  css  js  c++  java
  • [postman][API 测试]用Postman做RestAPI测试学习笔记

    痛点:最近有个API网关的兼容性测试任务,需要验证API是否可用,返回值符合预期,如果手工复制粘贴curl命令,繁琐且低效

    调研时发现了Postman 这个chrom插件,试用了2天后发现使用起来很方便,现总结如下:

    Postman简介:Postman是一种网页调试与发送网页http请求的chrome插件。我们可以用来很方便的模拟get或者post或者其他方式的请求来调试接口

    安装:通过chrome浏览器打开chrome网上应用店直接添加到chrome插件中 (可能需要番墙...)

    使用方式: 对照测试软件的接口API文档,设置对应的Header Body Test校验内容

    使用技巧总结:

    1.在body中用{{x}}使用环境变量,在test中environment.x调用环境变量


    2.注册环境变量:postman.setEnvironmentVariable("volumeid", responseJSON.data.elements[0].id); 这样就可以在下个case中直接调用了

    3.延时1-10秒
    GET https://postman-echo.com/delay/[1~10]

    4.延时指定的毫秒
    function pausecomp(millis)
    {
    var date = new Date();
    var curDate = null;
    do { curDate = new Date(); }
    while(curDate-date < millis);
    }
    //pausecomp(10000);
    pausecomp(environment['sleep-interval']);

    5.数组json处理
    //refer to https://stackoverflow.com/questions/42257293/extract-value-from-array-of-objects-in-postman
    postman.setEnvironmentVariable("volumeid", responseJSON.data.elements[0].id);

    6. 所有case写完后,可以统一Run (如果不加入try -catch处理,会停在第一个调用方法失败的地方)

    7. Test 标签页做校验

     1 var responseJSON;
     2 try { 
     3 responseJSON = JSON.parse(responseBody); 
     4 tests['response is valid JSON'] = true;
     5 }
     6 catch (e) { 
     7 responseJSON = {}; 
     8 tests['response is valid JSON'] = false;
     9 }
    10 
    11 if (responseJSON.code !== 0) return;
    12 // continue with other tests
    13 
    14 if (responseBody.has("attached")) return;
    15 if (responseBody.has("error_attach")) return;
    16 if (responseBody.has("error_detach")) return;
    17 if (responseBody.has("attaching")) return;
    18 if (responseBody.has("detaching")) return;
    19 
    20 tests["Create response code is 200"] = responseCode.code === 200;
    21 tests["get attached Status"] = responseBody.has("detached");
    22 tests["get attached Status"] = responseBody.has("host_ip");

    Postman遇到的坑:
    1. "snapshot_id":"{{snapshot_id}}" ,如果不加双引号,会报错: "detail": "invalid character 's' looking for beginning of value",Postman有时候不会自动处理双引号

  • 相关阅读:
    学渣逆袭回忆录:0序
    selenium和Firefox版本不兼容
    学习Python的第一课(简单的单元测试)
    C# 实现模拟登录功能,实现公共类分享。
    MVC之权限管理-网站开发之路
    程序员学习之路
    【数据结构】浅谈线性数据结构
    【数据结构】二叉堆
    tire 学习心得
    洛谷P1801 黑匣子
  • 原文地址:https://www.cnblogs.com/carol2000/p/7571427.html
Copyright © 2011-2022 走看看