postman 官方文档: https://learning.getpostman.com/docs/postman/scripts/test_examples/

之前只是使用postman做接口管理——将各个项目使用到的接口分类管理起来,用的时候手动改参数调用。这次项目连着跑三个接口,需要用到前一个接口的参数,还来回切平台,真的很麻烦,所以采用切换 environment 的方式去完善。

 变量的设置

方式1:上文中的手动输入设置

postman设置 RemoteIpAddress Postman设置参数_JSON

方式二:代码设置(对全局变量,全局变量针对所有环境有效)

postman设置 RemoteIpAddress Postman设置参数_后端_02

举个栗子(参考博主截图):

postman设置 RemoteIpAddress Postman设置参数_postman_03

postman设置 RemoteIpAddress Postman设置参数_测试工具_04

var jsonData = JSON.parse(responseBody);

// 设置全局变量

postman.setGlobalVariable("task_id", jsonData.data.task_id);  

// 设置环境变量

postman.setEnvironmentVariable("task_id", jsonData.data.task_id);

// 清除一个全局变量

postman.clearGlobalVariable("variable_key");

//清除一个环境变量

 postman.clearEnvironmentVariable("variable_key");


变量的使用

1、添加一个环境,右上角眼睛或者左边new菜单栏里的environment

postman设置 RemoteIpAddress Postman设置参数_测试工具_05

或者

postman设置 RemoteIpAddress Postman设置参数_测试工具_06

2、在添加环境界面设置环境名和环境里的变量,可以给变量初始值,也可以不给(可以后续走接口返回值设置该变量的值)

postman设置 RemoteIpAddress Postman设置参数_测试工具_07

postman设置 RemoteIpAddress Postman设置参数_JSON_08

3、变量的使用:通过形式:{{变量名}},当切换环境的时候(右侧环境名箭头下拉,有你保存的所有环境,可选择当前环境),这个变量则为不同的值。所以通过切换环境,我们可以批量改变一个请求中的多个参数

postman设置 RemoteIpAddress Postman设置参数_数据_09

Pre-requestScript 的使用

 对于环境变量和全局变量的使用,除了上面所讲的方法外,也可以用Pre-requestScript 方法。

以login接口为例,在"Pre-requestScript"中设置环境变量 "username", "password",在Body 中选取"form-data" 格式,输入所需的key-value, value即为变量{{username}}, {{password}}。

postman设置 RemoteIpAddress Postman设置参数_后端_10

Tests使用(断言)

所谓断言,主要用于测试返回的数据结果进行匹配判断,匹配成功返回PASS,失败返回FAIL。

下图方法一,直接点击右侧例子函数,会自动生成出现在左侧窗口脚本,只需修改数据即可。

postman设置 RemoteIpAddress Postman设置参数_后端_11

方法二:直接自己写脚本,如下图所示:

postman设置 RemoteIpAddress Postman设置参数_JSON_12

 常用tests用法如下: 

1.检查response body中是否包含某个string
tests["Body matches string"] = responseBody.has("string_you_want_to_search");
注意:"Body matches string" 需唯一。

2.检测JSON中的某个值是否等于预期的值
var data = JSON.parse(responseBody);
tests["Your test name"] = data.value === 100;
JSON.parse()方法,把json字符串转化为对象。parse()会进行json格式的检查是一个安全的函数。 
如:检查json中某个数组元素的个数(这里检测programs的长度)
var data = JSON.parse(responseBody);
tests["program's lenght"] = data.programs.length === 5;

3.检查response body是否与某个string相等

4.转换XML body为JSON对象
var jsonObject = xml2Json(responseBody);
tests["Body is correct"] = responseBody === "response_body_string";

5.测试response Headers中的某个元素是否存在(如:Content-Type)
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); 
//getResponseHeader()方法会返回header的值,如果该值存在
或者: 
tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
上面的方法,不区分大小写。下面的方法,要区分大小写。

6.验证Status code的值
tests["Status code is 200"] = responseCode.code === 200;

7.验证Response time是否小于某个值
tests["Response time is less than 200ms"] = responseTime < 200;

8.name是否包含某个值
tests["Status code name has string"] = responseCode.name.has("Created");

9.POST 请求的状态响应码是否是某个值
tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;

10.很小的JSON数据验证器
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);

传参进行接口测试

在接口测试过程中,有的时候需要构建多组同类型数据测试(正常数据,异常数据),当然没必要写多条测试用例,可使用CSV格式传参
举个例子,在测试用户登录接口的时候,需要用户输入相应的手机号和密码
那么,需要构建的测试数据有:手机号&密码正确,手机号正确&密码错误,手机号错误&密码正确,手机号为空&密码不为空,手机号正确&密码为空,手机&密码均为空。
如post请求:/api/login?phone={{phone}}&password={{password}}
a、可本地创建一个txt文档,并编辑数据,数据的头部必须参数名称保持一致,也就是phone和password,数据如下:

postman设置 RemoteIpAddress Postman设置参数_测试工具_13

b、然后选择批量运行,即collection runner,操作如下:

postman设置 RemoteIpAddress Postman设置参数_JSON_14

数据预览图,如下:

postman设置 RemoteIpAddress Postman设置参数_postman_15

c、最后点击运行就OK,可以在collection runner->Run Results查看运行结果。

压力测试

压力测试只能以文件夹的方式执行多个接口,不能单独执行。

postman设置 RemoteIpAddress Postman设置参数_测试工具_16

设置并发10次。执行后结果:

postman设置 RemoteIpAddress Postman设置参数_数据_17

要对 上传文件接口 进行压力测试, 参考: 像postman上传文件_Postman的Runner中使用formdata上传文件