本文讲述了如何使用Newman配合Postman进行自动化测试;
Postman结合Newman做自动化测试
在Postman通过Collection进行一组测试的基础上,Postman官方提供了Newman这个命令行工具,可以通过Newman对Collection导出的测试集进行测试
Newman的Github仓库:
https://github.com/postmanlabs/newman
关于Postman的用法以及Postman中的tests脚本编写见:
这里不多做介绍;
Newman安装
Newman依赖于node.js > v10,并且需要npm;
关于Node.js的安装参考Node官网:
安装Newman:
通过npm即可很方便的安装newman:
1 | npm install -g newman |
安装完成后,可以执行下面的命令测试是否成功安装:
1 | newman -version |
Newman使用
Newman支持两种使用方式,一种是通过命令行+参数的方式进行测试,另一种是编写js文件,并通过node运行测试,下面分别介绍这两种方式;
使用cli运行Newman
例如我从Postman中导出了api-pl-kylinkzhang-helloworld.proto.postman_collection.json的测试集以及对应的测试环境local-test.postman_environment.json;
则可以简单通过下面的命令进行整体测试:
1 | newman run api-pl-kylinkzhang-helloworld.proto.postman_collection.json --reporters cli --environment local-test.postman_environment.json |
上面的命令通过newman run collection.json指定Postman导出的Collection测试集;
通过--reporters指定测试结果输出的格式,如需要输出多种结果可使用逗号分割,类似于: --reporters cli, html
通过--environment environment.json指定Postman导出的测试环境文件;
执行完成后,测试接口和测试结果都会列出在Terminal中,如下:
1 | api/pl-kylinkzhang-helloworld.proto |
可以通过--reporters指定多种形式的输出,官方提供的格式有:cli, json, junit, progress and emojitrain;
第三方例如:newman-reporter-html提供了html的测试结果输出格式,可以使用npm install -g newman-reporter-html安装,并通过下面的命令生成测试结果:
1 | newman run api-pl-kylinkzhang-helloworld.proto.postman_collection.json --reporters cli,html --environment local-test.postman_environment.json --reporter-html-export result.html |
当然你也可以自己编写相应的reporter,详见:lib/reporters
更多的,官方提供的所有命令行参数如下:
1 | -h, --help output usage information |
可通过newman -h和newman run -h查看更多;
编写js运行Newman
如果你想更自由的定制自动化测试方案,可以通过编写js脚本并通过node执行来实现;
Newman提供了相当丰富的API供用户使用,具体可见:
https://github.com/postmanlabs/newman#api-reference
下面以一个简单的例子来展示如何编写js来调用Newman的API进行自动化测试;
1 | const newman = require('newman'); |
上面的js文件通过collection指定待测试数据集,reporters指定测试结果输出的格式,environment指定测试环境文件;
之后通过on方法指定各个阶段,以及各个阶段的回调函数。在整个测试过程中,在不同阶段会调用对应的回调函数;官方提供的阶段有:
| Event | Description |
|---|---|
| start | The start of a collection run |
| beforeIteration | Before an iteration commences |
| beforeItem | Before an item execution begins (the set of prerequest->request->test) |
| beforePrerequest | Before prerequest script is execution starts |
| prerequest | After prerequest script execution completes |
| beforeRequest | Before an HTTP request is sent |
| request | After response of the request is received |
| beforeTest | Before test script is execution starts |
| test | After test script execution completes |
| beforeScript | Before any script (of type test or prerequest) is executed |
| script | After any script (of type test or prerequest) is executed |
| item | When an item (the whole set of prerequest->request->test) completes |
| iteration | After an iteration completes |
| assertion | This event is triggered for every test assertion done within test scripts |
| console | Every time a console function is called from within any script, this event is propagated |
| exception | When any asynchronous error happen in scripts this event is triggered |
| beforeDone | An event that is triggered prior to the completion of the run |
| done | This event is emitted when a collection run has completed, with or without errors |
通过编写在测试不同阶段的回调函数,即可定制你自己的自动化测试方案;
例如上例中就通过调用mail模块封装的发送邮件接口完成了在测试未通过时向指定用户发送测试未通过邮件警告;
附录
如果觉得文章写的不错, 可以关注微信公众号: Coder张小凯
内容和博客同步更新~