接口测试 postman 接口测试

xinxi · 2019年04月10日 · 最后由 皮大大的豆花皮 回复于 2020年12月21日 · 7961 次阅读
本帖已被设为精华帖!

postman 使用

创建用例集

启动 postman 以后,会看到这个控制面板.

image

点击 Request 是创建一个 Request 测试请求,但是需要创建用例集保存这个请求.

image

点击 Collection 是创建一个用例集来保存测试请求.

创建 Collection 完成后,会在左侧生成用例集文件架,每次创建的测试接口都要保存到用例集中.

image

第一个接口测试

创建 get 请求为例,通常需要写 url、params、headers,会把 params 拼接到 url 末尾.

image

点击 send 按钮并且请求成功,会展示响应结果.

image

创建 post 请求为例,通常需要写 url、body、headers 等参数,body 参数格式一般是 form 或者 json 格式.具体 body 使用那个格式,需要按照接口文件中的参数.

image

接口断言

点击 Tests 编写测试断言

image

断言响应时间

pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});
// 断言响应事件小于200ms

断言状态码

pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([200,202]);
});
// 断言状态码200-202区间

断言响应中包含某个字符串

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

// 断言响应中包含"ok"

断言响应中的字段等于某个值

pm.test("message test", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData["message"]).to.eql("ok");
});

// 断言响应中"message" = ok"

断言响应中的字段不等于某个值

var jsonData = JSON.parse(responseBody);
tests["message不为bad"] = jsonData["message"] != "bad";

// 断言响应中"message" != bad"

断言响应中的列表长度

pm.test("data list test", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData["data"].length).to.eql(41);
});

// 断言响应中"list"的字段长度

断言响应中的列表中第几个元素的字段值

pm.test("data list 0 test", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData["data"][0]["time"]).to.eql("2018-11-28 17:27:41");
});

// 断言响应中"list 0的"的time字段的值

json schema 验证

tv4 是 postman 内置的 JSON Schema 验证库,参考:https://geraintluff.github.io/tv4/

responseBody 如下==:==

{
    "errCode": 0,
    "errMsg": "",
    "data": {
        "id": 3210,
        "title": "",
const customerSchema = {
  "type": "object",        
  "properties": {
    "errCode": {
        "type": "integer",
        "minimum": 0,
        "maximum": 3,
        "minLength": 2,
        "maxLength": 3
    },
    "errMsg": {"type": "string"},
  }
};

var customer = JSON.parse(responseBody);
// console.log(customer);
tests["Valid Data1"] = tv4.validate(customer, customerSchema);
//验证json中的errCode类型是integer,并且验证最小值和最大值区间、验证长度区间

以上是常用断言方法,更多使用参考:https://learning.getpostman.com/docs/postman/scripts/test_scripts/

测试前准备

发送请求之前往往需要准备数据,比如设置 header 中参数或者计算签名.

使用 Pre-request Script 可以编写一些准备数据.

image

在 header 头中引入刚刚设置{{timestamps}}环境变量.

image

可以看到 header 中已经填写了时间戳参数.

image

请求前编写加密算法

var username = "test";
var pwd = "123321";
var base64Str = CryptoJS.enc.Utf8.parse(username+pwd);
var token = CryptoJS.enc.Base64.stringify(base64Str);
postman.setGlobalVariable("token",token);
console.log(token);

// 使用账号+密码的base64位加密算法

加密生成的字符串

image

header 头中携带生成加密的 token 变量

image

服务端使用 base64 位解密

image

接口环境变量

image

image

image

接口参数化

全局变量

image

局部变量

使用{{}}作为变量

image

参数化文件

.csv 文件格式,第一行是变量名,后面是具体赋值.
image

选择参数化文件

image

接口参数传递

在登录接口的响应数据中获取 token 值.

image

把 token 传递给第二个接口中的 header 头中.

image

第二个接口中的 header 头中已经拿到了 token.

image

其他常用的方法

设置环境变量

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

设置全局变量

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

获取环境变量

pm.environment.get("variable_key");

获取全局变量

pm.globals.get("variable_key");

清除环境变量

pm.environment.unset("variable_key");

清除全局变量

pm.globals.unset("variable_key");

newman 使用

官方教程

https://learning.getpostman.com/docs/postman/collection_runs/command_line_integration_with_newman/

安装

npm install -g newman

运行

简单运行

newman run 接口测试.postman_collection.json

打印循环次数、请求次数、断言次数、耗时等,但是没有输出文件.

image

循环执行

newman run 接口测试.postman_collection.json -n 2

image

参数化

-d 是参数化文件

newman run 接口参数化测试.postman_collection.json -d 参数化数据.csv

image

报告

jenkins 持续集成

在 jenkins 中创建自由风格的 job

image

job 配置

构建 shell 配置

newman run 文件路径/接口测试.postman_collection.json
--reporters cli,html,json,junit
--reporter-json-export jsonOut.json
--reporter-junit-export xmlOut.xml
--reporter-html-export htmlOut.html

构建后报告配置参数

**/*.xml

image

image

image

image

学习帖子

Postman+Newman 简介和简单使用

https://www.jianshu.com/p/dd0db1b13cfc

postman 接口自动化,环境变量的用法详解(附 postman 常用的方法)

https://www.cnblogs.com/JHblogs/p/6418802.html

使用脚本动态生成签名参数

https://testerhome.com/topics/17488

postman 接口自动化,环境变量的用法详解(附 postman 常用的方法)

https://www.cnblogs.com/JHblogs/p/6418802.html

JSON Schema 介绍及应用

https://imweb.io/topic/56b1b4bb5c49f9d377ed8ee9

共收到 17 条回复 时间 点赞

图片全挂了

x 回复

多谢反馈 已经解决了

恒温 将本帖设为了精华贴 09月09日 10:41

详细介绍了 postman 的使用。

postman 确实是个成功的产品。

pm.test("data list test", function () {
var jsonData = pm.response.json();
pm.expect(jsonData["data"].length).to.eql(41);
});

// 断言响应中"list"的字段长度

这么知道 list 字段的长度呢?

simple [精彩盘点] TesterHome 社区 2019 年 度精华帖 中提及了此贴 12月24日 23:00
simple [精彩盘点] TesterHome 社区 2019 年 度精华帖 中提及了此贴 12月24日 23:00

很详细,学习了,谢谢!

很精辟,总结到位。复习了 谢谢

请问一下,postman 导出文件时,里面全局变量会自动也导出吗?

嘿Neal先森 回复

是的 自己导出导入试试看看

chen 回复

没有自动导出啊,报错了,全部没通过,后来我把环境、全局变量通过下载,才正常通过了。。。

精华,都是实用的干货

收藏(刚刚提交了,看不到?)

感谢分享,刚好要学习

小白表示很赞👍

能重新把图片更新下么?图片的都看不到

图片都看不到,可以更新下嘛

学学看看 回复

我也不能自动显示出来

需要 登录 後方可回應,如果你還沒有帳號按這裡 注册