51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 497|回复: 0
打印 上一主题 下一主题

接口用例之Postman执行

[复制链接]
  • TA的每日心情
    无聊
    昨天 09:06
  • 签到天数: 941 天

    连续签到: 3 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2023-6-8 10:36:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    接口用例:用例编号、模块、测试标题、优先级、前置条件、URL、请求方法、请求参数、预期结果。
      宠物商店:实践接口平台。
      一、创建测试集
      宠物商店----宠物

      二、创建请求
      1、查询宠物
      GET请求,https://petstore.swagger.io/v2/pet/findByStatus

      填入必填项params,status:available,下面我全局变量改成乐sold。
      增加断言:
        // 断言状态
        pm.test("响应状态码为 200", function () {
            pm.response.to.have.status(200);
        });

        // 断言业务
        pm.test("响应体中包含宠物的 id 信息", function () {
            pm.expect(pm.response.text()).to.include("id");
        });
       pm.test("响应体中包含宠物的 状态", function () {
           pm.expect(pm.response.text()).to.include("sold");
       });



      2、新增宠物
      POST请求,https://petstore.swagger.io/v2/pet/
      增加Body---raw---json
        {
            "id": 9223372016900012345,
            "category": {
                "id": 0,
                "name": "cat"
            },
            "name": "miao",
            "photoUrls": [
                "string"
           ],
           "tags": [
               {
                   "id": 5,
                   "name": "cute"
               }
           ],
           "status": "available"
       }


      增加断言
        pm.test("响应状态码 200", function () {
            pm.response.to.have.status(200);
        });

        // 全量字符匹配
        // https://www.sojson.com/yasuo.html
        // 压缩并转移义
        pm.test("响应体与请求参数完全一致", function () {
            pm.response.to.have.body("{\"id\":9223372016900012345,\"category\":{\"id\":0,\"name\":\"cat\"},\"name\":\"miao\",\"photoUrls\":[\"string\"],\"tags\":[{\"id\":5,\"name\":\"cute\"}],\"status\":\"available\"}");
       });


      3、更新宠物信息
      PUT请求,https://petstore.swagger.io/v2/pet/
      增加Body---raw---json
        {
            "id": 9223372016900012345,
            "category": {
                "id": 0,
                "name": "cat"
            },
            "name": "喵喵",
            "photoUrls": [
                "string"
           ],
           "tags": [
               {
                   "id": 5,
                   "name": "cute"
               }
           ],
           "status": "available"
       }


      增加断言
        {
            "id": 9223372016900012345,
            "category": {
                "id": 0,
                "name": "cat"
            },
            "name": "喵喵",
            "photoUrls": [
                "string"
           ],
           "tags": [
               {
                   "id": 5,
                   "name": "cute"
               }
           ],
           "status": "available"
       }


      4、删除宠物
      DELETE请求,https://petstore.swagger.io/v2/pet/自己新增的宠物id
      增加断言
        // 需要已经有这个宠物id,才能删除

        pm.test("响应状态码 200", function () {
            pm.response.to.have.status(200);
        });

        pm.test("业务错误码为 200", function () {
            var jsonData = pm.response.json();
            pm.expect(jsonData.code).to.eql(200);
       });

       pm.test("message 为删除宠物的id信息", function () {
           var jsonData = pm.response.json();
           pm.expect(jsonData.message).to.eql("9223372016900012345");
       });


      三、运行测试集

      查看结果,点击进入后可以查看每个的请求结果。

      附:常用的断言
      ·验证响应状态码
      · 验证响应体中是否包含某个字符串
      · 验证 JSON 中的某个值是否等于预期的值
      · 验证响应体是否与某个字符串完全相同
      · 验证响应头信息中的 Content-Type 是否存在
      · 验证响应时间是否小于某个值
        // Status Code:Code is 200
        // 验证响应状态码
        pm.test("响应状态码为 200", function () {
            pm.response.to.have.status(200);
        });

        // Response Body:contains string
        // 验证响应体中是否包含某个字符串
        pm.test("响应体中包含预期的字符串", function () {
           pm.expect(pm.response.text()).to.include("doggie");
       });

       // Response Body:JSON value check
       // 验证 JSON 中的某个值是否等于预期的值
       pm.test("宠物名称为 doggie", function () {
           var jsonData = pm.response.json();
           pm.expect(jsonData[0].name).to.eql("doggie");
       });

       // Response Body:Is equal to a string
       // 验证响应体是否与某个字符串完全相同
       pm.test("响应体正确", function () {
           pm.response.to.have.body("response_body_string");
       });

       // Response Body:Content-Type header check
       // 验证响应头信息中的 Content-Type 是否存在
       pm.test("Content-Type is present", function () {
           pm.response.to.have.header("Content-Type");
       });

       // Response time is less than 200ms
       // 验证响应时间是否小于某个值
       pm.test("Response time is less than 200ms", function () {
           pm.expect(pm.response.responseTime).to.be.below(200);
       });


      四、变量
      Postman 中变量的种类与作用域
      ·Data:在测试集中上传的数据
      · Environment:环境范围
      · Collection:集合范围
      · Global:全局范围
      · Local:在脚本中设置的变量
      定义变量
      · 全局变量:Environments -> Globals
      · 测试集变量:测试集页面 -> Variables
      · 环境变量:Environments -> +
      在 Pre-request Script 和 Tests 脚本中使用封装好的语句获取或者设置对应变量
        // 获取全局变量
        var status = pm.globals.get("status");
        // 输入到控制台
        console.log(status)

        // 获取测试集变量
        var petId = pm.collectionVariables.get("petId");
        // 获取环境变量
        var url = pm.environment.get("baseURL");

       // 设置全局变量
       pm.globals.set("status", "sold");
       // 设置测试集变量
       pm.collectionVariables.set("petId", 0);
       // 设置环境变量
       pm.environment.set("baseURL", "");


       1)全局变量,Environments -> Globals

      请求 URL, Params 参数或 Body 表格或JSON/XML 文本中通过 {{变量名}} 使用。

      附:可使用 console 调试脚本

      重置全局变量:pm.globals.set("status", "sold");
      2)测试集变量,测试集页面 -> Variables

      修改 新增宠物 的 json 和 Tests,运行成功。
        {
            "id": {{petId}},
            "category": {
                "id": 0,
                "name": "cat"
            },
            "name": "miao",
            "photoUrls": [
                "string"
           ],
           "tags": [
               {
                   "id": 5,
                   "name": "cute"
               }
           ],
           "status": "available"
       }


        pm.test("响应状态码 200", function () {
            pm.response.to.have.status(200);
        });

        var petId = pm.collectionVariables.get("petId");

        // 全量字符匹配
        // https://www.sojson.com/yasuo.html
        // 压缩并转移义
       pm.test("响应体与请求参数完全一致", function () {
           pm.response.to.have.body("{\"id\":"+ petId + ",\"category\":{\"id\":0,\"name\":\"cat\"},\"name\":\"miao\",\"photoUrls\":[\"string\"],\"tags\":[{\"id\":5,\"name\":\"cute\"}],\"status\":\"available\"}");
       });


      更新宠物信息、删除宠物,这两个接口的 json 和 Tests,也是一样的
      3)环境变量,Environments -> +

      所有请求地址相应改一下{{baseURL}},如请求地址拼接:{{baseURL}}/pet/findByStatus?status={{status}}
      变量优先级:尽量不使用重名变量:
      优先级从高至低为:Data -> Enviroment -> Collection -> Global -> Local

    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-5-1 13:33 , Processed in 0.065460 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表