写的api多了之后或者接手别人的项目以后,对api的运维也会比较多,特别是在测试环境,种种因素会致使接口出现不符合预期,这个时候当产品啊、测试啊,都跑过来骚扰你的时候,你的第一个反应是本身执行一下,看是否是真的接口有问题,而后再具体分析。html
一般是拼接好接口地址,构造好参数,而后请求api,看看返回结果。这类动做作多了以后一般比较烦人,特别是最后发现是接口ok的。git
因而就想找个rest api的自动化测试工具,方便本身排查问题。github
可以批量导入swaggernpm
可以本身构造测试接口json
能批量runapi
能输出reportapp
最好可以alertless
jmeter——网上找了一圈,首先入眼的是jmeter,可是因为界面界面有点粗糙,学习成本有点高,故暂时没有考虑。运维
soupui——看起来是老牌的工具,不过因为免费版不支持导出report,也就放弃了ide
dredd——这个看起来不错,能够支持swagger的,不过本身粗略试一下,没执行成功,也就先放弃了
postman——这个之前就有装过,只是没发现深挖它的功能,如今一看,挺简单的,容易上手,也支持swagger,而后就是它了
这个功能是我最看重的,左上角有个import的按钮,能够选择"Import From Link",输入接口的swagger api docs的地址,好比:http://192.168.99.100:8080/scm/v2/api-docs,而后导入就能够了。
保存一个请求的时候,能够选择已有的collection,或者新建一个。collection能够对等为test suite。
对于要跑自动化测试的,必需要设置tests这里的脚本,否则即便run,也没有啥意义。最简单最经常使用的两行脚本以下:
tests["Status code is 200"] = responseCode.code === 200; tests['Response time is less than 500ms'] = responseTime < 500;
一个是断言http的状态码,一个是断言响应时间。
随便选择collection的一个http请求,而后点击右侧的设置按钮,Manage Environments,而后能够定义环境变量,能够定义dev、prod两套,分别设置对应的环境的api的host,这样就不用重复设置api请求了。在url中用{{varname}}来引用变量,假设varname就是你设置的一个变量名。
左上角有个runner图标,点一下弹出COLLECTION RUNNER界面。在这里就要进行批量自动测试的地方,选择environment,而后跑一下。
导出配置
在collection那里,export,选择Collection V2,导出为json。若是使用了environment,则须要导出该environment的json配置。
安装newman
sudo npm install -g newman
查看newman版本
newman -version 3.4.3
命令行执行
newman run demo.postman_collection.json --reporters cli,html --environment dev.postman_environment.json --reporter-html-export result.html
命令行结果以下:
┌─────────────────────────┬──────────┬──────────┐ │ │ executed │ failed │ ├─────────────────────────┼──────────┼──────────┤ │ iterations │ 1 │ 0 │ ├─────────────────────────┼──────────┼──────────┤ │ requests │ 22 │ 0 │ ├─────────────────────────┼──────────┼──────────┤ │ test-scripts │ 22 │ 0 │ ├─────────────────────────┼──────────┼──────────┤ │ prerequest-scripts │ 0 │ 0 │ ├─────────────────────────┼──────────┼──────────┤ │ assertions │ 44 │ 6 │ ├─────────────────────────┴──────────┴──────────┤ │ total run duration: 28s │ ├───────────────────────────────────────────────┤ │ total data received: 312.29KB (approx) │ ├───────────────────────────────────────────────┤ │ average response time: 1245ms │ └───────────────────────────────────────────────┘ # failure detail 1. AssertionFai… Response time is less than 1000ms at assertion:2 in test-script inside "XXXX" of "app1" 2. AssertionFai… Response time is less than 1000ms at assertion:2 in test-script inside "XXXX" of "app2" 3. AssertionFai… Response time is less than 1000ms at assertion:2 in test-script inside "XXXX" of "app1" 4. AssertionFai… Response time is less than 1000ms at assertion:2 in test-script inside "XXXX" of "app3" 5. AssertionFai… Response time is less than 1000ms at assertion:2 in test-script inside "XXXX" of "app2" 6. AssertionFai… Status code is 200 at assertion:1 in test-script inside "XXXX" of "app1"
同时会生成result.html报告。
构建选择Execute Windows batch command——输入上面的命令就能够了
Publish JUnit test result report——jenkins有个Publish JUnit test result report能够用来解析junit的xml测试报告。要用这个的话,命令行得输出junit的report
newman run demo.postman_collection.json --reporters cli,html,junit --environment dev.postman_environment.json --reporter-html-export result.html --reporter-junit-export junit-result.xml