接口测试之postman断言方法(五)

postman是一款功能强大的网页调试和HTTP请求的Chrome插件。postman里提供完整的断言方法,无需我们会写,只要我们能够使用即可。就像我们不用了解空调的内部结构,只要会使用就好。

postman->Tests->右侧单击所需断言方法即可添加成功,再进行修改使用。

接口测试之postman断言方法(五)_第1张图片

断言结果PASS和FAIL

接口测试之postman断言方法(五)_第2张图片


下面具体介绍断言方法,并结合具体的接口实例。

查询发布会接口URL:http://127.0.0.1:8000/api/get_event_list/?eid=6&name='ipad发布会',response body如下:

接口测试之postman断言方法(五)_第3张图片

1、设置/清除一个全局变量 (Set/Clear a global variable)

    对应脚本:

    pm.globals.set("variable_key", "variable_value");

    postman.clearGlobalVariable("variable_key");

    参数:需要设置/清除的变量的key,需要设置的变量value

接口测试之postman断言方法(五)_第4张图片

2、设置/清除一个环境变量(需要在多个环境测试同一个接口)

    Set/Clear an environment variable

    对应脚本:

    pm.environment.set("variable_key", "variable_value");

    pm.environment.unset("variable_key");

    参数:需要清除的环境变量的key,需要设置的变量value

接口测试之postman断言方法(五)_第5张图片

3、response包含内容(Response body:Contains string)

对应脚本如下面两种:

//发布会查询接口response body包含name字段,注意断言名称可自行定义,tests["自定义"]

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

//简化方法,发布会查询接口response body的limit字段是否包含1000

tests["校验limit是否为1000"] =responseBody.has("1000");

接口测试之postman断言方法(五)_第6张图片

4、将xml格式的response转换成son格式( Response body:Convert XML body to a JSON Object)

对应脚本:  var jsonObject = xml2Json(responseBody);

接口测试之postman断言方法(五)_第7张图片

5、response等于预期内容( Response body:Is equal to a string)

对应脚本:

    tests["Body is correct"] = responseBody === "response_body_string";

    参数:预期response

接口测试之postman断言方法(五)_第8张图片

6、json解析key的值进行校验(Response body:JSON value check)

    对应脚本:

    tests["Args key contains argument passed as url parameter"] = 'test' in responseJSON.args

    参数:test替换被测的值,args替换被测的key

7、检查response的header信息是否有被测字段

    Response headers:Content-Type header check

    对应脚本:tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");

    参数:预期header

接口测试之postman断言方法(五)_第9张图片

8、响应时间判断

    Response time is less than 200ms

    对应脚本: tests["Response time is less than 200ms"] = responseTime < 200;

    参数:响应时间

接口测试之postman断言方法(五)_第10张图片

9、判断状态码

      Status code:Code is 200

      对应脚本:tests["Status code is 200"] = responseCode.code != 400;

      参数:状态码

接口测试之postman断言方法(五)_第11张图片

10、检查code name 是否包含内容

      Status code:Code name has string

      对应脚本: tests["Status code name has string"] = responseCode.name.has("Created");

      参数:预期code name包含字符串


11、成功的post请求

      Status code:Successful POST request

      对应脚本:  tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;

12、微小验证器

      Use Tiny Validator for JSON data           

      对应脚本:

        var schema = {

        "items": {

        "type": "boolean"

            }

        };

        var data1 = [true, false];

        var data2 = [true, 123];

        console.log(tv4.error);

        tests["Valid Data1"] = tv4.validate(data1, schema);

        tests["Valid Data2"] = tv4.validate(data2, schema);

        参数:可以修改items里面的键值对来对应验证json的参数

你可能感兴趣的:(接口测试之postman断言方法(五))