zoukankan      html  css  js  c++  java
  • postman接口测试03_添加断言

    前言

    postman进行接口测试时,需要添加断言,校验接口返回是否符合断言预期。可在Tests模块添加断言脚本,常用的脚本模板可直接在右侧点击添加。

    断言实例

    调用某接口后返回如下:

     接口测试时,可对以上接口返回进行接口请求状态码、“code”:1、“msg”:"success"进行校验,判定接口返回符合预期。

    该接口断言脚本如下:

     备注:运行接口,查看断言结果时,在Runner中运行才会进行断言校验,直接send运行不会进行断言校验。

    常用断言

    1,校验接口请求状态码

    模板名:Status code:Code is 200

    模板脚本:

    pm.test("Status code is 200", function () {
        pm.response.to.have.status(200);
    });

    2,校验接口返回中包含xx字符串

    模板名:Response body:Contains string

    模板脚本:

    pm.test("Body matches string", function () {
        pm.expect(pm.response.text()).to.include("string_you_want_to_search");
    });

    3,校验接口返回json中的字段值

    模板名:Response body:JSON value check

    模板脚本:

    pm.test("Your test name", function () {
        var jsonData = pm.response.json();
        pm.expect(jsonData.value).to.eql(100);
    });

    备注:校验时需修改字段名&字段值,例接口返回如下:

    1,校验简单字段值

    以校验上图code=1为例,如下:

    pm.test("Your test name", function () {
        var jsonData = pm.response.json();
        pm.expect(jsonData.code).to.eql(1);
    });

    2,校验多层嵌套字段值:

    以校验上图list[0]中的word为例,如下:

    pm.test("Your test name", function () {
        var jsonData = pm.response.json();
        pm.expect(jsonData.data.list[0].word).to.eql("儿童节亿元补贴");
    });

    4,校验接口返回字段值=xx字符串

    模板名称:Response body:is equal to a string

    模板脚本:

    pm.test("Body is correct", function () {
        pm.response.to.have.body("response_body_string");
    });
    5,校验接口响应时间<xxms

    模板名称:Response time is less than 200ms

    模板脚本:

    pm.test("Response time is less than 200ms", function () {
        pm.expect(pm.response.responseTime).to.be.below(200);
    });
  • 相关阅读:
    MVC4数据访问EF查询linq语句的时候报错找不到表名问题
    以下各节已定义,但尚未为布局页“~/Views/Shared/_Layout.cshtml”呈现:“Scripts”。
    将函数实现放在头文件中
    const 不兼容的类型限定符问题
    Ubuntu 16.04重装后grub rescue> 终端模式修复方法
    Ubuntu 16.04 编译VTK7.1
    区域生长算法的一种C++实现
    Win7 U盘安装Ubuntu16.04 双系统
    Win7、Ubuntu双系统卸载Ubuntu系统
    AES加密补位填充的一个问题
  • 原文地址:https://www.cnblogs.com/mini-monkey/p/12919113.html
Copyright © 2011-2022 走看看