- 安装与更新
- 发送第一个请求
- 创建第一个集合(collections)
- 界面布局
- 帐号
- 同步
- 设置
- 请求(Request)
- 响应(Response)
- 历史记录(History)
- API 请求疑难解答
- 调试和日志
- 授权(Authorization)
- Cookies
- 证书(Certificates)
- 捕获 HTTP 请求(Capturing HTTP requests)
- Interceptor 扩展
- 生成代码段(Generate code snippets)
- 发出 SOAP 请求
- 创建集合(Creating collections)
- 分享集合(Sharing collections)
- 管理集合(Managing collections)
- 使用 Markdown 添加描述
- 示例(Examples)
- 数据格式
- 脚本简介
- 预请求脚本(Pre-request scripts)
- 测试脚本
- 测试示例(Test examples)
- 分支和循环(Branching and looping)
- 沙盒(Sandbox)
- 变量(Variables)
- 管理环境(Manage environments)
- 管理全局变量(Manage globals)
- 开始运行一个集合
- 在集合运行器中使用环境
- 使用数据文件(Working with data files)
- 运行多次迭代(Running multiple iterations)
- 构建工作流程(Building workflows)
- 共享集合运行(Sharing a collection run)
- 调试集合运行(Debugging a collection run)
- 与 Newman 集成
- 与 Jenkins 集成
与 Newman 集成
与 Newman 的命令行集成
- Newman
Newman 是 Postman 的命令行集合运行器。它允许您直接从命令行运行和测试 Postman 集合。它以可扩展性为基础构建,以便您可以轻松地将其与您的持续集成服务器集成并构建系统。
纽曼主要特性与 Postman 的功能类似,并允许您运行集合,只需在 Postman 应用程序的集合运行中执行集合。
Newman 发布在 NPM registry 和 GitHub 中 。
在 Linux,Windows 或 Mac 上开始使用
Newman 建立在 Node.js. 要运行 Newman,请确保已安装 Node.js。Node.js 可以 下载并安装 在 Linux,Windows 和 Mac OSX 上。
一旦安装了 Node.js,Newman 只是一个命令。从系统的全局 npm 命令安装 Newman,让您从任何地方运行它。
$ npm install -g newman
运行 Newman 的最简单方法是使用集合来运行它。您可以从文件系统运行任何集合文件。请参阅 集合文档 ,了解如何导出集合以作为文件共享。
$ newman run mycollection.json
您也可以将一个集合作为 URL 传递。请参阅 集合文件 ,了解如何以 URL 的形式共享文件。您的集合可能使用环境变量。要提供一组伴随的环境变量,请从 Postman 导出模板 ,并使用该 -e
标志运行它们。
$ newman run https://www.getpostman.com/collections/cb208e7e64056f5294e5 -e dev_environment.json
选项
纽曼提供了丰富的选项来自定义运行。可以通过使用该 -h
标志来运行选项列表。
$ newman run -h
Options:
Utility:
-h, --help output usage information
-v, --version output the version number
Basic setup:
--folder [folderName] Specify a single folder to run from a collection.
-e, --environment [file|URL] Specify a Postman environment as a JSON [file]
-d, --data [file] Specify a data file to use either json or csv
-g, --global [file] Specify a Postman globals file as JSON [file]
-n, --iteration-count [number] Define the number of iterations to run
Request options:
--delay-request [number] Specify a delay (in ms) between requests [number]
--timeout-request [number] Specify a request timeout (in ms) for a request
Misc.:
--bail Stops the runner when a test case fails
--silent Disable terminal output
--no-color Disable colored output
-k, --insecure Disable strict ssl
-x, --suppress-exit-code Continue running tests even after a failure, but exit with code=0
--ignore-redirects Disable automatic following of 3XX responses
使用该 -n
选项设置运行集合的迭代次数。
$ newman run mycollection.json -n 10 # runs the collection 10 times
要提供不同的数据集,即每次迭代的变量,可以使用该数据 -d
来指定 JSON 或 CSV 文件。例如,如下所示的数据文件将运行 2 次迭代,每次迭代使用一组变量。
[{
"url": "http://127.0.0.1:5000",
"user_id": "1",
"id": "1",
"token_id": "123123",
},
{
"url": "http://postman-echo.com",
"user_id": "2",
"id": "2",
"token_id": "899899",
}]
$ newman run mycollection.json -d data.json
上述变量组的 CSV 文件将如下所示:
url, user_id, id, token_id
http://127.0.0.1:5000, 1, 1, 123123123
http://postman-echo.com, 2, 2, 899899
默认情况下,Newman 退出状态码为 0,如果一切正常运行,即没有任何例外。持续集成工具可以对这些退出代码进行响应,并相应地通过或失败构建。您可以使用``-bail 标志告诉 Newman 停止测试用例错误,状态代码为 1,然后可以由 CI 工具或构建系统进行选择。
$ newman run PostmanCollection.json -e environment.json --bail newman
示例集合与失败的测试
→ Status Code Test
GET https://echo.getpostman.com/status/404 [404 Not Found, 534B, 1551ms]
1\. response code is 200
┌─────────────────────────┬──────────┬──────────┐
│ │ executed │ failed │
├─────────────────────────┼──────────┼──────────┤
│ iterations │ 1 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ requests │ 1 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ test-scripts │ 1 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ prerequest-scripts │ 0 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ assertions │ 1 │ 1 │
├─────────────────────────┴──────────┴──────────┤
│ total run duration: 1917ms │
├───────────────────────────────────────────────┤
│ total data received: 14B (approx) │
├───────────────────────────────────────────────┤
│ average response time: 1411ms │
└───────────────────────────────────────────────┘
# failure detail
1\. AssertionFai… response code is 200
at assertion:1 in test-script
inside "Status Code Test" of "Example Collection with
Failing Tests"
所有测试和请求的结果可以导出到一个文件中,然后导入 Postman 进行进一步分析。使用 JSON 报告和一个文件名将流程输出保存到一个文件中。
$ newman run mycollection.json --reporters cli,json --reporter-json-export outputfile.json
注意: Newman 允许您使用 Postman 支持的所有 库和对象 来运行测试和预请求脚本。
文件上传
Newman 还支持文件上传。为了正常工作,要上传的文件必须放置在集合中指定的相对位置。例如,对于以下集合:
{
"variables": [],
"info": {
"name": "file-upload",
"_postman_id": "9dbfcf22-fdf4-f328-e440-95dbd8e4cfbb",
"description": "A set of `POST` requests to upload files as form data fields",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
"item": [
{
"name": "Form data upload",
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"var response = JSON.parse(responseBody).files[\"sample-file.txt\"];",
"",
"tests[\"Status code is 200\"] = responseCode.code === 200;",
"tests[\"File was uploaded correctly\"] = /^data:application\\/octet-stream;base64/.test(response);",
""
]
}
}
],
"request": {
"url": "https://echo.getpostman.com/post",
"method": "POST",
"header": [],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "file",
"type": "file",
"enabled": true,
"src": "sample-file.txt"
}
]
},
"description": "Uploads a file as a form data field to `https://echo.getpostman.com/post` via a `POST` request."
},
"response": []
}
]
}
该文件 sample-file.txt
必须存在于与集合相同的目录中。收集可以照常运行。
$ newman run file-upload.postman_collection.json
文档库
Newman 从一开始就建成了一个文档库,以便以各种方式进行扩展和使用。您可以在 Node.js 代码中使用它:
var newman = require('newman'); // require newman in your project
// call newman.run to pass `options` object and wait for callback
newman.run({
collection: require('./sample-collection.json'),
reporters: 'cli'
}, function (err) {
if (err) { throw err; }
console.log('collection run complete!');
});
定制报告
想要生成针对特定用例的集合运行报告,自定义报告将派上用场。例如,当请求(或它的测试)失败时输出响应日志,等等。
建立自定义报告
自定义报告是具有表单名称的 Node 模块 newman-reporter-<name>
。创建自定义报告:
- 导航到您选择的目录,并创建一个空白的 npm 软件包
npm init
。 添加一个
index.js
导出以下形式的函数的文件:function (emitter, reporterOptions, collectionRunOptions) { // emitter is is an event emitter that triggers the following events: https://github.com/postmanlabs/newman#newmanrunevents // reporterOptions is an object of the reporter specific options. See usage examples below for more details. // collectionRunOptions is an object of all the collection run options: https://github.com/postmanlabs/newman#newmanrunoptions-object--callback-function--run-eventemitter };
- 发布您的报告使用
npm publish
,或使用您的报告本地 查看使用说明 。
有作用范围内的报告软件包名称例如 @myorg/newman-reporter-<name>
也被支持。使用报告的例子可以在 使用报告的例子中 找到。
使用自定义报告
为了使用自定义的报告,必须首先安装。例如,使用 Newman 的报告 :
安装报告包。
npm install newman-reporter-teamcity
请注意,包的名称是形式 newman-reporter-<name>
, <name>
是报告的实际名称。如果 Newman 全局安装,则本地安装应为全局,否则为局部安装。运行全局安装 npm install ...
的 -g
标志。
要使用本地(未发布)的报告,请运行该命令 npm install <path/to/local-reporter-directory>
。
通过 CLI 或通过编程方式使用已安装的报告。这里, newman-reporter
在选项中指定报告名称时,不需要前缀。
有作用范围报告程序包必须用范围前缀指定。例如,如果你的包名是 @myorg/newman-reporter-name
,你必须指定记者 @myorg/name
。
CLI:
newman run /path/to/collection.json -r myreporter --reporter-myreporter-<option-name> <option-value> # The option is optional
编程方式:
var newman = require('newman');
newman.run({
collection: '/path/to/collection.json',
reporters: 'myreporter',
reporter: {
myreporter: {
'option-name': 'option-value' // this is optional
}
}
}, function (err, summary) {
if (err) { throw err; }
console.info('collection run complete!');
});
在上述两种情况下,报告选项是可选的。
有关详细信息的完整列表,请参阅 Newman README 。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论