美文网首页
API测试-PostMan使用

API测试-PostMan使用

作者: 寒冰蘑菇 | 来源:发表于2020-08-07 08:21 被阅读0次

    API测试知识分享


    程序测试

    分类标准 分类
    按照测试方式划分 黑盒测试
    白盒测试
    按照执行过程划分 静态测试
    动态测试
    按照操作者划分 手动测试
    自动测试
    安装阶段划分 单元测试(模块测试)
    集成测试
    系统测试
    验收测试

    程序测试的原则

    1. IPO测试 (测试用例应由测试输入数据、测试执行步骤、预期输出结果构成)
    2. 独立测试 (测试者不能是开发人员)
    3. 合法和非合法原则 (测试应该包括合法参数和非法参数的校验)
    4. 覆盖原则 (测试用例应该尽可能覆盖程序编码)
    5. 回归测试原则 (测试用例应该可以被用来进行回归测试)
    6. 错误不可避免原则 (程序必然有bug)

    API测试介绍

    • 通过工具或代码方式去调用特定的API,获取输出,并记录系统的响应,测试接口可能存在的问题。

    • 属于集成测试的一部分。

    测试过程

    测试过程 内容
    数据准备 准备测试需要的元数据,包括非法参数,合法参数,正常数据,异常数据
    编写用例基类 编写测试基类(加载资源、初始化环境)
    用例执行 在工具或者环境中使用准备的数据,执行测试用例
    任务的编排 在复杂业务场中,编排基础测试用例,测试复杂业务功能

    测试领域

    负载测试
    安全测试
    功能测试
    模糊测试

    API testing 常用工具

    image
    1. SoapUI

      • 使用拖放、点击可以轻松生成测试 service的功能/负载/符合性测试。
      • 该工具既可作为一个桌面应用软件使用,也可利用插件集成到Eclipse,maven2.X,Netbeans 和Intellij中使用
      • 异步测试
      • 使用来自文件和数据库的数据进行强大的数据驱动测试
      • 使用RESTful mock创建模拟服务
      • 有免费版,也有付费版
    2. Postman :

      • 模拟各种 HTTPrequests.
      • 支持自动化测试
      • 支持mock功能
      • 内置测试脚本语言
      • 设定变量与环境
      • 生成Api文档(需要登录)
      • 团队工作空间(需要登录)
    3. 其他 各个工具功能缺点分析

    Postman提供的功能解读

    1. 工作窗口

    工作区界面.PNG
    • Api集合区 (collections)

    • 工作区 (workspace)

    • 配置区 (Setting)

    • 运行区 (runner)

    • 控制台(console)

    2. 基础用例编写

    • 创建一个Request (Building requests)
    • 使用环境变量 (Using variables)
    • 编写脚本 (Writing Scripts)
      • 请求前执行脚本 (Pre-request Script)
      • 请求后执行脚本 (test script )
      • 执行流程


      • 支持语言: node.js 、Javascript 、postman
    • 使用证书 (Working with certificates)

    Postman脚本语言

    1. 自动生成数据

      1. 添加变量

        var timestamp = Date.parse(new Date())
        var module_name = namespace+"/wx_"+timestamp
        postman.setEnvironmentVariable("auto_module_name", module_name );
        pm.globals.set("variable_key", "variable_value");
        pm.environment.set("array", JSON.stringify(array, null, 2));
        pm.collectionVariables.set(variableName:String, variableValue:String);
        
      2. 动态变量

        //随机uuid、时间戳
        $guid 一个uuid-v4风格GUID
        $timestamp    当前UNIX时间戳(以秒为单位)
        $randomDateFuture 未来的随机日期时间
        $randomDatePast   随机过去的日期时间
        //随机IP地址,ipv4、ipv6
        $randomIP 随机的IPv4地址
        //文字,数字和颜色
        $randomAbbreviation   随机缩写
        $randomHexColor   随机十六进制值
        $randomColor  随机颜色
        $randomAlphaNumeric   随机字母数字字符
        $randomInt    1至1000之间的随机整数
        $randomFirstName  随机名字
        //电话,地址和位置
        $randomCity   随机城市名称
        $randomCountry    随机国家
        $randomLatitude   随机纬度坐标
        $randomLongitude  随机经度坐标
        //图片
        $randomAvatarImage    随机头像    
        $randomSportsImage    随机运动图像的URL  
        //金融
        $randomBankAccount    随机的8位数字银行帐号
        //口头禅
        $randomCatchPhrase    随机的流行语
        $randomCatchPhraseDescriptor  随机标语描述符
        
    2. 校验返回值

      1. 使用pm.test()

        pm.test("Status test", function () {
            pm.response.to.have.status(200);
            pm.expect(pm.environment.get("env")).to.equal("production");
        });
        
      2. 使用ChaiJS BDD语法

        pm.test("子节点存在?", function () {
            pm.expect(jsonData.items[0].steps).to.not.be.empty
        });
        
      3. 使用js和node.js

        console.log(response.json());
        

        支持的模块:

        支持的Nodejs模块.PNG
    1. 使用异步功能

      pm.sendRequest("https://postman-echo.com/get", function (err, response) {
          console.log(response.json());
      });
      
    2. 使用TV4和AJV进行json模式验证

    3. 编码、解码

    4. 内置的函数

    postman.setNextRequest('Delay 10s');//设置等待时间,最大10s
    ```

    1. 可使用的内置库

      可用库.PNG

    3. 自动化测试用例编排

    • 代码示例
    /**********Pre-request**********/
    //设置namespace和moduleName
    var namespace = "wx_autotest_module"
    postman.setEnvironmentVariable("auto_name_space", namespace);
    var timestamp = Date.parse(new Date())
    var module_name = namespace+"/wx_"+timestamp
    postman.setEnvironmentVariable("auto_module_name", module_name );
    
    /**********Tests***********/
    //基础校验
    pm.test("请求返回状态", function () {
        pm.response.to.have.status(201);
    });
    pm.test("请求时间小于1s", function () {
        pm.expect(pm.response.responseTime).to.be.below(1000);
    });
    
    //自定义校验部分
    ////自定义通用校验
    function testFaild(discription){
        pm.test(discription,function(){
            pm.expect(2).to.equal(1)
        })
    }
    function testSeccuss(discription ){
        pm.test(discription)
    }
    
    var jsonData 
    error_str = "新增module"
    
    function conventResponse(){
        jsonData = JSON.parse(responseBody);
        if (jsonData == null){
            testFaild("返回body没有数据")
        }else if (jsonData.error_msg != null ){
             testFaild(jsonData.error_msg)
        }
    }
    
    ////自定义校验
    function customFunction(){
        pm.test("返回生成结果"+jsonData.name, function () {
            pm.expect(jsonData.name).to.not.be.undefined
        });
        if (jsonData.name != null ){
            postman.setEnvironmentVariable("module_name", jsonData.name );
        }
    }
    
    ////自定义校验执行
    conventResponse()
    customFunction()
    
    
    • 可设置Mock

    • 可设置Monitors

    • 运行自动化测试脚本

      运行.PNG
    • 查看结果
    运行结果.PNG

    4. 使用MockService

    可以添加MockService, 需要登录才能使用该功能

    5. 使用Monitor

    可以添加Monitor, 需要登录才能使用该功能


    Go语言测试框架简介

    已有测试框架:

    [httpexpect]https://github.com/gavv/httpexpect
    gorequest
    baloo
    gofight
    frisby
    forest
    restit
    httptesting
    http-test
    go-json-rest

    Httpexpect 使用Demo:

    package example
    
    import (
        "net/http"
        "net/http/httptest"
        "testing"
    
        "github.com/gavv/httpexpect/v2"
    )
    
    func TestFruits(t *testing.T) {
        // post form encoded from struct or map
        e.POST("/form").WithForm(structOrMap).
            Expect().
            Status(http.StatusOK)
    }
    
    // set individual fields
    e.POST("/form").WithFormField("foo", "hello").WithFormField("bar", 123).
        Expect().
        Status(http.StatusOK)
    
    // multipart form
    e.POST("/form").WithMultipart().
        WithFile("avatar", "./john.png").WithFormField("username", "john").
        Expect().
        Status(http.StatusOK)
    

    简介的使用端到端HTTP和REST API测试。

    httpexpect是在net / http和几个实用程序包之上的一组用于HTTP请求以及HTTP响应和有效负载断言的可链接构建器。

    工作流程:

    • 逐步建立HTTP请求。
    • 检查HTTP响应。
    • 递归检查响应负载。

    相关文章

      网友评论

          本文标题:API测试-PostMan使用

          本文链接:https://www.haomeiwen.com/subject/qgmprktx.html