美文网首页测试postman
『政善治』Postman — 18.NewMan的使用(一)

『政善治』Postman — 18.NewMan的使用(一)

作者: 繁华似锦Fighting | 来源:发表于2021-03-17 10:23 被阅读0次

前面向大家介绍了Postman的用法,我们发现这些操作都离不开Postman 的客户端,我们知道服务器一般都是Linux系统,那么如何在Linux系统下运行我们的Postman测试集脚本(json文件)呢,谷歌公司为了解决这个问题,引入了 NewMan工具。

1、NewMan工具的介绍

Newman是Postman的命令行,是一种Collection测试集运行程序。NewMan基于Node.js开发,它使您可以直接从命令行轻松运行和测试Postman测试集。它在构建时考虑了可扩展性,因此您可以轻松地将其与持续集成服务器集成并构建系统。

简单的说:

  • Postman使用NewMan命令行工具,完成Collection测试集脚本的执行。

  • NewMan是专门用于Postman进行自动化测试和持续集成的CLI工具(command-line interface,命令行界面)。

官方文档地址:https://www.npmjs.com/package/newman

2、NewMan的安装

(1)安装

首先需要安装Node.js,请看https://blog.csdn.net/Liuyuelinjiayou/article/details/110499056文章中关于Node.js的下载安装。

完成Node.js安装之后,在命令行中输入npm install -g newman自动完成NewMan的安装。

提示:

如果NewMan下载不了,如卡在下面不动

C:\Users\L>npm install newman -g
[..................] / rollbackFailedOptional: verb npm-session 8cb76e6aae9ea432 

或者出现下载失败,如下图:

我们可以更换npm命令使用国镜像,例如:淘宝。

# 1.获取npm命令当前使用的镜像库
C:\Users\L>npm config get registry
https://registry.npmjs.org/

# 2.设置成淘宝镜像库,并查看(修改的是.npmrc文件中的内容)
C:\Users\L>npm config set registry http://registry.npm.taobao.org/

C:\Users\L>npm config get registry
http://registry.npm.taobao.org/

# 3.下载NewMan工具
C:\Users\L>npm install newman -g
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
F:\DevInstall\nodejs\node_global\newman -> F:\DevInstall\nodejs\node_global\node_modules\newman\bin\newman.js
+ newman@5.2.2
added 157 packages from 200 contributors in 17.027s
# 特别要记住上面newman的安装路径
# F:\DevInstall\nodejs\node_global\newman

# 4.还原npm命令的镜像库
C:\Users\L>npm config set registry https://registry.npmjs.org/

C:\Users\L>npm config get registry
https://registry.npmjs.org/

(2)验证NewMan环境:

在CMD命令行中执行newman -v

但是出现如下情况。

C:\Users\L>newman -v
'newman' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

# 也找不到newman工具的位置
C:\Users\L>where newman
信息: 用提供的模式无法找到文件。

解决办法:进入到NewMan的安装目录(上边安装NewMan的时候有显示)

# 进入到NewMan的安装目录
# F:\DevInstall\nodejs\node_global
F:\DevInstall\nodejs\npm newman -v

# 执行newman -v命令查看newman的安装版本
F:\DevInstall\nodejs\node_global>newman -v
5.2.2

也可以把F:\DevInstall\nodejs\node_global配置到环境变量中。

这样在任何目录下都可以直接执行NewMan命令了。

(3)NewMan卸载命令

npm uninstall -g newman

3、NewMan执行Postman测试集

以前面文章的综合练习为例。

(1)导出collections

在collection测试集选项菜单中选择Export,导出为.json格式的文件。


然后选择默认推荐的Collection v2.1b版本即可。

点击Export,导出综合练习.postman_collection.json文件。(可以自行重命名)

(2)使用NewMan执行Collection

使用NewMan执行Collection测试集导出的.json文件。

通过NewMan命令行运行测试集,命令行中输入newman run 导出的json文件名即可。

执行过程如下图:

说明:

  • 第一部分:测试集名称和执行请求的列表,绿色对勾表示断言通过,红色文件表示断言失败。

  • 第二部分:测试集执行结果总结。

    iterations:迭代次数。
    requests:请求个数。
    test-scripts:请求后置脚本个数。
    prerequest-scripts:请求前置脚本个数。
    assertions:断言个数。

    total run duration:总运行时间。

    total data received:接收到的总数据大小。

    average response time:平均响应时间。

  • 第三部分:失败断言的列表,以及断言失败的详情。

相关文章

网友评论

    本文标题:『政善治』Postman — 18.NewMan的使用(一)

    本文链接:https://www.haomeiwen.com/subject/rpigfltx.html