[TOC]
<br>
### 一般項目結構
* Collection 項目(測試集)
* Folder 模塊
* request1 請求1
* request2 請求2
### Collection 介紹
我們可以稱之為‘測試骨架’或‘測試集’。從工具外觀上,像是一個文件夾,`用于組織和管理接口用例`相關信息。實際上它是一個可執行文件,是所有API和Postman內置工具的核心。是所有內置工具的`執行的開始`,包括:MOCK、文檔、測試、監測、發布。

### 創建 Collection


- 測試集名稱:一般可以采用項目名稱
- 描述文檔:描述項目的信息
- 前置腳本:在這個測試集下的每個接口,請求發送之前執行的腳本,一般用于參數構造處理
- 斷言測試:在這個測試集下的每個接口響應后,都會執行的斷言
- 變量設置:在這個測試集下的所有接口請求,都可以共享的變量
### 分享 Collection
當我們需要與其他團隊成員分享Collection時,我們有多鐘分享的形式,下面只介紹幾種常見的。
- 通過分享URL,使其他成員可以通過URL能訪問到Collection


- 通過注冊賬號,共享項目
- 通過導出Collection文件,實現傳播


這里導出到`D://tmp.postman_collection.json`,下面命令行執行會用到。
### 運行 Collection
當我們運行一個Collection后,在Colection中的requests會逐一順序執行,這在我們做API自動化測試中,起著非常好的作用。
#### 通過app中的“Runner”按鈕運行


運行結果報告如下:

#### 通過 `Newman` 命令行工具運行
Newman 是一個Collection Runner命令行工具,它是建立在Node.js上的,運行Newman之前,需確保我們已經安裝了Node.js
安裝后Node.js后,通過npm進行Newman安裝
```cmd
npm install -g newman
```
**通過Collection 文件運行**,命令行運行命令如
```cmd
$ newman run D://tmp.postman_collection.json
```
運行結果如下:
```cmd
newman
tmp
→ postman-echo.com/post
POST postman-echo.com/post [200 OK, 642B, 739ms]
→ postman-echo.com/get?foo1=bar1&foo2=bar2
GET postman-echo.com/get?foo1=bar1&foo2=bar2 [200 OK, 539B, 238ms]
┌─────────────────────────┬──────────┬──────────┐
│ │ executed │ failed │
├─────────────────────────┼──────────┼──────────┤
│ iterations │ 1 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ requests │ 2 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ test-scripts │ 0 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ prerequest-scripts │ 0 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ assertions │ 0 │ 0 │
├─────────────────────────┴──────────┴──────────┤
│ total run duration: 1049ms │
├───────────────────────────────────────────────┤
│ total data received: 553B (approx) │
├───────────────────────────────────────────────┤
│ average response time: 488ms │
└───────────────────────────────────────────────┘
```
**通過Collection分享URL運行**,格式如:
```cmd
$ newman run https://www.getpostman.com/collections/cb208e7e64056f5294e5
```
**更多newman參數**
```cmd
$ newman run -h
```
參數如下:
```cmd
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, --globals [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
```
<hr style="margin-top:100px">
:-: 
***微信掃一掃,關注“python測試開發圈”,獲取更多測試開發分享!***
- 前言
- Fiddler01-抓包原理介紹與配置
- Fiddler02-菜單功能介紹
- Fiddler03-輕松玩轉Fiddler
- Fiddler04-進階使用FiddlerScript
- Fiddler05-使用FiddlerScript對微信文章互動量進行監控
- Postman01-介紹與安裝
- Postman02-HTTP請求與響應
- Postman03-Collection管理與運行
- Postman04 -變量詳解
- Postman05-初級腳本使用
- Postman06-實例小結篇
- JMeter01-JMeter就是這么簡單
- JMeter02-一個完整實戰包你學會使用JMeter
- JMeter03-在JMeter中使用BeanShell編程
- JMeter04-圖形化和非圖形化運行JMeter
- JMeter05-生成美觀的HTML測試報告
- JMeter06-JMeter+Jenkins實戰
- JMeter07-解析session與cookie