Postman - Running collections on the command line with Newman

官方文档

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

OptionDetails
-h--helpOutput usage information
-v--versionOutput the version number

Basic setup

OptionDetails
--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

OptionDetails
--delay-request [number]Specify a delay (in ms) between requests [number]
--timeout-request [number]Specify a request timeout (in ms) for a request

Misc.

OptionDetails
--bailStops the runner when a test case fails
--silentDisable terminal output
--color offDisable colored output (auto|on|off) (default: "auto")
-k--insecureDisable strict ssl
-x--suppress-exit-codeContinue running tests even after a failure, but exit with code=0
--ignore-redirectsDisable automatic following of 3XX responses
--verboseShow detailed information of collection run and each request sent

一个详细的postman介绍:

使用postman做接口测试笔记_lang_niu的专栏-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值