官方文档
Running collections on the command line with Newman | Postman Learning Center
1.下载Nodejs
根据不同的系统下载对应的Nodejs
Download | Node.js
2.下载Newman
在npm目录下执行下面的命令
$ npm install -g newman
3.准备好对应的postman collection
{
"info": {
"_postman_id": "postman_id",
"name": "APITest,
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "GetList",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"Status code is 200\", function(){",
" pm.response.to.have.status(200);",
"});",
"var jsonData=JSON.parse(responseBody);",
"tests[\"Check response status value is 0\"] = jsonData.code===0;",
"tests[\"Check response msg is success\"] = jsonData.msg===\"success\";"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [
{
"key": "email",
"value": "{{email}}",
"type": "text"
}
],
"url": {
"raw": "https://{{server_name}}/v1/report/list",
"protocol": "https",
"host": [
"{{server_name}}"
],
"path": [
"v1",
"report",
"list"
]
}
},
"response": []
}
]
}
4.执行postman collection
$ newman run APITest.postman_collection.json
指定data file,json或者csv
$ newman run APITest.postman_collection.json -d postman_value.json
[{
"server_name":"reports_list",
"email":"xiaoxiao@mail.com"
}]
指定输出结果 outputfile.json
$ newman run APITest.postman_collection.json -d postman_value.json --reporters cli,json --reporter-json-export outputfile.json
outputfile.json
{
"collection": {
"_": {
"postman_id": "bdb00378"
},
"item": [
{
"id": "b06fdfgrtr",
"name": "CustomTemplateList",
"request": {
"url": {
"protocol": "https",
"path": [
"v1",
"report",
"list",
],
"host": [
"{{server_name}}"
],
"query": [],
"variable": []
},
"header": [
"key": "email",
"value": "{{email}}"
}
],
"method": "GET"
},
"response": [],
"event": [
{
"listen": "test",
"script": {
"id": "3hhg67-fxdfg",
"type": "text/javascript",
"exec": [
"pm.test(\"Status code is 200\", function(){",
" pm.response.to.have.status(200);",
"});",
"var jsonData=JSON.parse(responseBody);",
"tests[\"Check response status value is 0\"] = jsonData.code===0;",
"tests[\"Check response msg is success\"] = jsonData.msg===\"success\";"
],
"_lastExecutionId": "608eb6b1-4e5b-4c5d-8e41-8f"
}
}
]
},
"environment": {
"id": "d981313aeere4d-diodsi",
"values": []
},
"globals": {
"id": "72diur478-fdkhduf",
"values": []
},
"run": {
"stats": {
"iterations": {
"total": 1,
"pending": 0,
"failed": 0
},
"items": {
"total": 7,
"pending": 0,
"failed": 0
},
"scripts": {
"total": 7,
"pending": 0,
"failed": 0
},
"prerequests": {
"total": 7,
"pending": 0,
"failed": 0
},
"requests": {
"total": 7,
"pending": 0,
"failed": 0
},
"tests": {
"total": 7,
"pending": 0,
"failed": 0
},
"assertions": {
"total": 21,
"pending": 0,
"failed": 0
},
"testScripts": {
"total": 7,
"pending": 0,
"failed": 0
},
"prerequestScripts": {
"total": 0,
"pending": 0,
"failed": 0
}
},
"timings": {
"responseAverage": 34.142857142857146,
"responseMin": 19,
"responseMax": 114,
"responseSd": 32.669026709139175,
"dnsAverage": 0,
"dnsMin": 0,
"dnsMax": 0,
"dnsSd": 0,
"firstByteAverage": 0,
"firstByteMin": 0,
"firstByteMax": 0,
"firstByteSd": 0,
"started": 1631952565869,
"completed": 1631952566793
},
"executions": [
...
],
"transfers": {
"responseTotal": 17062
},
"failures": [],
"error": null
}
}
newman其他Options
Utility
Option | Details |
---|
-h ,?--help | Output usage information | -v ,?--version | Output the version number |
Basic setup
Option | Details |
---|
--folder [folderName] | Specify a single folder to run from a collection. | -e ,?--environment [file|URL] | Specify a Postman environment as a JSON [file] | -d ,?--iteration-data [file] | Specify a data file to use either json or csv | -g ,?--globals [file] | Specify a Postman globals file as JSON [file] | -n ,?--iteration-count [number] | Define the number of iterations to run |
Request options
Option | Details |
---|
--delay-request [number] | Specify a delay (in ms) between requests [number] | --timeout-request [number] | Specify a request timeout (in ms) for a request |
Misc.
Option | Details |
---|
--bail | Stops the runner when a test case fails | --silent | Disable terminal output | --color off | Disable colored output (auto|on|off) (default: "auto") | -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 | --verbose | Show detailed information of collection run and each request sent |
|