一、环境准备
- allure的安装
- 准备newman-reporter-allure
- newman安装
一.allure的安装
1.在github上下载allure包,解压
https://github.com/allure-framework/allure2/releases
2.环境变量的配置
(1)添加环境变量
vi ~/.bash_profile
(2)加入环境变量,路径为自己allure 的路径就好
(3)配置完成后,执行命令让环境变量生效
source ~/.bash_profile
(4)验证allure环境变量是否配置成功
allure --version
到此,allure安装配置成功。
二、newman-reporter-allure的安装
这个比较简单,没什么好说的。
使用命令行
npm install -g newman-reporter-allure
进行安装就好
三、newman的安装
这个是核心部分,但安装也是很简单的
npm install -g newman
主要是学习newman的使用,这些网上可以搜出一大堆
二、正式开始使用Newman
一、Postman的数据导出
1.通过集合导出所有请求
2.环境变量的导出
最后就得到两个json文件
二、newman的使用
1.简单使用newman
newman run json文件 [options]
例如: newman run OnlyMainPhoto.postman_collection.json
单纯的不要环境的脚本可以这样简单跑一跑,但实际中,应该都需要环境变量吧。
-e 参数可以把我们之前导出的环境变量用上。
-n iteration-count :指定迭代次数
newman run OnlyMainPhoto.postman_collection.json -e env.json -n 10
这样就可以了。
2.如果想仅运行集合里几个文件
可以使用 --folder [filename]来选择想运行的文件
newman run OnlyMainPhoto.postman_collection.json --folder 注册+approve -e env.json
同理,如果运行多个文件,则添加多个–folder
newman run OnlyMainPhoto.postman_collection.json --folder 注册+approve --folder 注册+delete -e env.json
3.运行中如果有环境变量的更改,那就再把环境导出去保存,下一次运行就好
通过 --export-environment env.json 又把环境更改了
newman run OnlyMainPhoto.postman_collection.json --folder 注册+approve --folder 注册+delete -e env.json --export-environment env.json
4.添加证书
--ssl-client-cert [cert] --ssl-client-key [key] client-passphrase [passphrase]
我使用的是无passphrase的,如下:
newman run OnlyMainPhoto.postman_collection.json --folder 注册+approve -e env.json --export-environment env.json --ssl-client-cert client_cert.pem --ssl-client-key client_nopwd_key.pem
注意:如果集合里请求包含多个证书,那么就需要建立一个证书的json文件
然后在运行的时候添加
--ssl-client-cert-list [json文件]
newman run OnlyMainPhoto.postman_collection.json --folder 后台-login --folder 注册+approve -e env.json --ssl-client-cert-list ssl-list.json --export-environment env.json
到这里newman运行成功了。接下来就是生成测试报告了。
有很多种测试报告可以生成,这里用的allure,也是比较流行的一个吧。
三、allure测试报告的生成
1.newman run的时候添加参数
-r allure --reporter-allure-export [文件保存路径]
newman run OnlyMainPhoto.postman_collection.json --folder 后台-login --folder 注册+approve -e env.json --ssl-client-cert-list ssl-list.json --export-environment env.json -r allure --reporter-allure-export ./allure_report
这里直接保存当前文件下了。可以自己更改路径。
这保存的全是json和txt格式的文件,然后通过allure命令生成测试报告
2.生成测试报告
allure generate [原文件] -o [目的文件] --clean 生成allure测试报告
3.测试报告生成了,接下来打开就好
allure open [文件名]
Report successfully generated to allure_html
pofdeMacBook-Pro:Postman pof$ allure open allure_html/
Starting web server...
2021-07-16 12:24:19.495:INFO::main: Logging initialized @404ms to org.eclipse.jetty.util.log.StdErrLog
Server started at <http://192.168.50.232:55206/>. Press <Ctrl+C> to exit
ps. 这里的请求应该是你postman命名好的。但是newman-report-allure会弄成一堆乱码
可以参考:
https://www.cnblogs.com/landhu/p/14419555.html
可能有许多命令我没有用到,大家可以自己去搜啦。