postman+jenkins+newman做接口测试的持续集成
一。安装node.js
下载地址:https://nodejs.org/en/ ,双击后根据提示下一步安装完成
在dos窗口中输入 :node -v 查看,如下图所示表示安装好了
二.重新打开cmd,输入如下指令:
npm install --global --production windows-build-tools
安装这些依赖需要一些时间,耐心等待安装完成。
三.然后安装Newman:
输入命令:npm install -g newman
安装会持续几分钟,直到出现下面界面,安装结束
通过查看newman版本测试安装是否成功
输入命令:newman -v
安装newman-reporter-html
npm
install
newman-reporter-html
本机调试脚本
1.1了解命令行执行脚本
可以查看github上面的命令说明https://github.com/postmanlabs/newman#configuring-reporters、https://github.com/postmanlabs/newman-reporter-html#readme
run XX.json #执行脚本
---reporters html --reporter-html-export /路径 #导出执行结果到此路径,若指定输出报告,则只会在dos窗口输出执行结果
-e XX.json #带上环境变量
四.安装Postman
Postman功能(https://www.getpostman.com/features)
主要用于模拟网络请求包
快速创建请求
回放、管理请求
快速设置网络代理
postman 下载地址:https://www.getpostman.com
安装后,导出Collection
在postman中执行测试用例通过后,将postman中的用例导出,并用Newman执行,如下:
导出环境变量:
导出全局变量:
五.Newman 执行脚本
命令行运行collection
cmd进入到collection的json文件的目录下,输入指令
newman run postmanEcho.postman_collection.json
例子1,通过newman 运行postman导出的test1.json文件,并生成多种测试报告(json,junit的xml,html):
newman run c:\test1.json –reporters cli,html,json,junit –reporter-json-export jsonOut.json –reporter-junit-export xmlOut.xml –reporter-html-export htmlOut.html
例子2,运行https://www.getpostman.com/collections/cb0cea0af1467c8008fb(postman生成的 )中的所有api,并使用env.json作为环境变量和globals.json作为全局变量,并使用外部data.csv作为外部数据,最后设置了接口请求超时时间为5S 。
newman run https://www.getpostman.com/collections/cb0cea0af1467c8008fb
–environment env.json –iteration-data data.csv –globals globals.json –timeout-request 5000
例子3
newman run C:\11\testpm.postman_collection.json –reporter-html-export C:\11\htmlOut.html -n 10
迭代10次
六.Newman语法
1、运行一个集合
newman run 集合名
newman run url地址
2、-e,–environment 指定环境文件路径或者url
-e的用法是如果我们的集合设置到环境变量,那么但是运行集合文件是报错的,需要使用-e指定才能运行集合,比如运行测试搜索集合
newman run d:\test.postman_collection.json -e d:\test.postman_environment.json
3、-g,–globals 指定全局变量的文件地址或url 用法如-e
4、-d,—-iteration-data 指定用于迭代的数据源文件路径
newman run d:\test.postman_collection.json -e d:\test.postman_environment.json -d d:\testdata.csv
5、-n,–iteration-count 指定迭代次数
newman run 集合名 -n 10(迭代次数)
6、–folder 运行集合中指定的文件夹
如集合下存在文件(前端接口),使用该命名,则只运行集合中前端接口文件下的接口
7、–export-environment 导出该运行集合中的环境变量文件 可指定路径和名称
newman run d:\test.postman_collection.json -e d:\test.postman_environment.json -d d:\testdata.csv —-export-environment d:\test
–export -environment d:\test 导入环境变量文件名称为test 存储在d盘下
注意:
将该文件导入postman之后还是与导出前的文件名相同,比如此处【test.postman_environment.json】导出前对应的环境变量为【test】,那么test导入之后的命名还是【test】,同样,导出集合也是如此。
8、–export-globals 导出全局环境变量
9、–export-collection 导出集合文件
10、–timeout (ms) 设置整个集合运行完成执行的时间
11、–timeport-request (ms) 指定等待请求返回响应的时间
12、–timeout-script (ms)指定等待脚本执行完成的时间
七.结合Jenkins
作用:
1.持续、自动地构建/测试软件项目。
2.监控一些定时执行的任务。
Jenkins下载地址 https://jenkins.io/download/
步骤一:在Jenkins 机器上安装Newman
步骤二:搭建Jenkins环境,并新建个自由风格的Job
步骤三:进入http://localhost:8080
步骤四:构建选择Execute Windows batch command,并输入newman 运行命令
点击完成后,在点击立即构建
补充:
如果在生成reporter过程中报错提示
newman: “html” reporter could not be loaded.
runnpm install newman-reporter-html
需要加载html模块,可以在命令行中运行以下命令:
npm install -g newman-reporter-html(需要以管理员权限来运行)
-g
是指全局安装,将安装包放在 /usr/local 下或者node的安装目录,以便可以直接在命令行里使用;不带则是安装在当前目录。
在Jenkins中添加HTML Publisher Plugin插件
如果生成的html报告比较丑的话,可以在 jenkins中添加 : 系统管理 》 脚本命令行 运行 :System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "") 即可解决
运行成功最后生成测试报告~~
jenkins-系统设置-节点管理
点击新增,添加nodejs、npm环境变量
乱码解决办法:
解决方法:直接新建一个系统变量名为JAVA_TOOL_OPTIONS ;变量值为-Dfile.encoding=UTF-8 ;
保存并重启jenkins;可以通过查看系统管理--系统信息--file.encoding(管理文件中的编码)已经修改,由GBK修改为UTF-8,但是sun.jnu.encoding(管理文件名的编码)没有修改
(如果想修改sun.jnu.encoding ,将之前设置改为JAVA_TOOL_OPTIONS ;变量值为-sun.jnu.encoding=UTF-8 即可)两者只能改其一