使用postman+newman+python做接口自动化测试

postman是一款API调试工具,可用于测试接口,相类似的工具还有jmeter、soupUI。通过postman+newman+python可以批量运行调试接口,达到自动化测试的效果。

1、PostMan安装
共有两种方式,一种是chrome浏览器上的插件,一种是postman客户端。我使用的是postman客户端。
1)在Chrome浏览器怎么安装Postman
https://www.cnblogs.com/mafly/p/postman.html
2)安装Postman客户端
a、下载软件https://www.getpostman.com/apps
b、安装
2、使用
1)发送请求、查看响应
2)环境变量、全局变量
环境变量:只作用于设置的环境
设置环境变量:pm.environment.set("variable_key", "variable_value");
获取环境变量:pm.environment.get("variable_key");
全局变量:作用于所有环境
设置全局变量:pm.globals.set("variable_key", "variable_value");
获取全局变量:pm.globals.get("variable_key");
使用例子:
var data=JSON.parse(responseBody);
var act=data.data.accessToken;
postman.setGlobalVariable("accessToken", act);
postman.setGlobalVariable("userId", data.data.userId);
postman.setGlobalVariable("refreshToken", data.data.refreshToken);
var afterUrl="?access_token="+act+"&userId="+data.data.userId;
pm.globals.set("afterUrl", afterUrl);
console.log(afterUrl)
使用变量:
在使用的变量地方用 {{variableName}}代替
具体查看文档:https://www.getpostman.com/docs/postman/environments_and_globals/variables
3)设置断言
tests["Your test nickName"] = data.data.nickName === "2589" //响应内容 nickName =2589
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
}); //返回为200

var responseJSON=JSON.parse(responseBody);
tests['response matches the data posted'] = (responseJSON.data && responseJSON.data.length === 10);
//返回data数据共10条
4)调试console
需要在postman客户端,点击 view->show postman console 调出
在test或 Pre-request Script中写脚本打印出有疑问的值
console.log(variableName); 之后运行请求
5)collection
需要在postman客户端,点击collection->Runner ,运行
具体查看文档:https://www.getpostman.com/docs/postman/collection_runs/starting_a_collection_run

6)具体使用如下图

\

7)导出json文件

2、newman安装

官方帮助文档地址:https://www.npmjs.com/package/newman
1)需要安装nodejs,并配置好环境
2)打开控制台,运行:npm install -g newman
3)校验是否安装成功,运行:newman --version
Newman 执行脚本
  Newman在3版本后做了比较大的改动,但是运行命令越来越简单如下:

newman run <collection-file-source> [options]
run 后面跟上要执行的json文件或者URL(json 和 URL 都由postman导出生成),再后面跟一些参数,例如环境变量,测试报告,接口请求超时时间等等。最后给两个完整的例子做参考:
newman run D:/Buddy_Product_Enviroment.postman_collection.json --reporters cli,html,json,junit --reporter-json-export D:/jsonOut.json --reporter-junit-export D:/xmlOut.xml --reporter-html-export D:/htmlOut.html

3、使用python脚本执行newman

扫描二维码关注公众号,回复: 1803075 查看本文章
# coding=utf-8
import time
import os
class postmanApiTest:
#运行postman生成报告
#通过newman

def postman(self):
jSONfname = 'D:/htmlOut' + time.strftime('%Y-%m-%d', time.gmtime())+'.html'
# cmd = 'newman run ‪D:/Buddy_Test_Enviroment.postman_collection.json --reporters cli,html,json,junit --reporter-html-export '+jSONfname
cmd='newman run D:/Buddy_Product_Enviroment.postman_collection.json --reporters cli,html,json,junit --reporter-json-export D:/jsonOut.json --reporter-junit-export D:/xmlOut.xml --reporter-html-export D:/htmlOut.html'
os.system(cmd)
print('------------------------------------------------------------')
print(jSONfname)
if os.path.isfile(jSONfname):
return jSONfname
print(jSONfname)
else:
return False
if __name__ == '__main__':
a=postmanApiTest()
a.postman()

4、最终生成报告如下:

 

猜你喜欢

转载自www.cnblogs.com/LinxiHuang/p/9247286.html