jenkins+newman实现接口测试

安装newman

1
2
3
4
5
6
7
8
[root@public-service jenkins]# npm install -g newman
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated uuid@3.4.0: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.
/home/.npm-global/bin/newman -> /home/.npm-global/lib/node_modules/newman/bin/newman.js
+ newman@5.2.4
added 128 packages from 187 contributors in 20.745s
[root@public-service jenkins]# newman -v
5.2.4

通过postman生成json文件

![image-20210629094214541](E:\Typora\jenkins\image-20210629094214541.png” />

通过newman请求该接口进行测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[root@public-service jenkins]# newman run -r cli test.postman_collection.json 
newman: could not find "html" reporter
ensure that the reporter is installed in the same directory as newman
run `npm install newman-reporter-html`

newman

test

→ https://test.com/doctor-api/v2_0/optimalmall/getProduct
POST https://test.com/doctor-api/v2_0/optimalmall/getProduct [200 OK, 5.84KB, 868ms]

┌─────────────────────────┬────────────────────┬───────────────────┐
│ │ executed │ failed │
├─────────────────────────┼────────────────────┼───────────────────┤
│ iterations │ 1 │ 0 │
├─────────────────────────┼────────────────────┼───────────────────┤
│ requests │ 1 │ 0 │
├─────────────────────────┼────────────────────┼───────────────────┤
│ test-scripts │ 0 │ 0 │
├─────────────────────────┼────────────────────┼───────────────────┤
│ prerequest-scripts │ 0 │ 0 │
├─────────────────────────┼────────────────────┼───────────────────┤
│ assertions │ 0 │ 0 │
├─────────────────────────┴────────────────────┴───────────────────┤
│ total run duration: 892ms │
├──────────────────────────────────────────────────────────────────┤
│ total data received: 5.47KB (approx) │
├──────────────────────────────────────────────────────────────────┤
│ average response time: 868ms [min: 868ms, max: 868ms, s.d.: 0µs] │
└──────────────────────────────────────────────────────────────────┘

newman命令

1
2
3
4
5
6
7
8
newman run  用例json文件路径
-e 环境变量文件地址
-g 全局变量文件地址
--delay-request 请求间的delay时长,单位为毫秒
--folder 文件夹路径,运行指定文件夹下的用例
-n,--iteration-count number,collection执行的次数
--export-environment path,执行用例后将环境变量导出
--export-globals path,执行用例后将全局变量导出

导出报告

Cli

在console中生成报告

![image-20210629094559461](E:\Typora\jenkins\image-20210629094559461.png” />

HTML

作为一种external报告方式,html reporter需要安装:

1
2
3
4
5
6
npm install -g newman-reporter-html

[root@public-service jenkins]# newman run test.postman_collection.json -r html
[root@public-service jenkins]# cd newman/
[root@public-service newman]# ls
newman-run-report-2021-06-29-01-35-46-951-0.html

![image-20210629094719897](E:\Typora\jenkins\image-20210629094719897.png” />