👏

Postman CLI の様々なオプション

2025/03/03に公開

この記事はPostman Advent Calendar 2024 の 12月15日分の記事です。

前回こちらの記事でPostman CLI についてご紹介しました。
https://qiita.com/KameMan/items/727ae719b13ca6a0b3ee

今日はいくつかのオプション機能を試していきます。

さっそくやってみる

まずはこの記事の内容を終わらせておきます。
https://qiita.com/KameMan/items/727ae719b13ca6a0b3ee

環境変数 -e オプション

Postman CLI は以下で紹介したNewmanと異なり、プライベート環境へのアクセスと環境変数を取り込めることが特徴です。
https://qiita.com/KameMan/items/1ac6b0b090af6b171e8a

まずは以下の通りCLIという環境名で変数をセットします。
image.png
image.png
いつものことですが 保存 を忘れずに。

APIコールで{{dummy}}と変数名を指示するとtestがPOSTされます。
image.png

次にこの変数をExportします。
image.png

{
	"id": "115dd96c-d078-4e51-8d35-64166c719b99",
	"name": "CLI",
	"values": [
		{
			"key": "dummy",
			"value": "test",
			"type": "default",
			"enabled": true
		}
	],
	"_postman_variable_scope": "environment",
	"_postman_exported_at": "2024-12-15T05:30:14.815Z",
	"_postman_exported_using": "Postman/11.22.2-241209-1418"
}
postman collection run <Collection ID> -e <先ほどダウンロードしたjson> 

を実行するとdummytestに置換されます。

環境変数 --env-var オプション

--env-varは動的に変数を宣言し値を投入することが出来ます。

postman collection run <Collection ID> --env-var dummy=test

-r オプション

https://learning.postman.com/docs/postman-cli/postman-cli-reporters/
Reporterという機能を使うことでファイルに実行結果を出力することが出来ます。

postman collection run <Collection ID> --env-var dummy=test -r json
{
  "run": {
    "meta": {
      "collectionId": "b9cac9b0-896a-48e3-a2f7-5aa1d4536d63",
      "collectionName": "New Collection",
      "started": 1734241142438,
      "completed": 1734241142994,
      "duration": 556
    },
    "summary": {
      "iterations": {
        "executed": 1,
        "errors": 0
      },
      "executedRequests": {
        "executed": 1,
        "errors": 0
      },
      "prerequestScripts": {
        "executed": 0,
        "errors": 0
      },
      "postresponseScripts": {
        "executed": 0,
        "errors": 0
      },
      "tests": {
        "executed": 0,
        "failed": 0,
        "passed": 0,
        "skipped": 0
      },
      "timeStats": {
        "responseAverage": 449,
        "responseMin": 449,
        "responseMax": 449,
        "responseStandardDeviation": 0
      }
    },
    "executions": [
      {
        "iterationCount": 0,
        "requestExecuted": {
          "id": "d1a7ddea-a28d-49de-beda-488000054ca9",
          "name": "https://cli.requestcatcher.com/",
          "url": {
            "protocol": "https",
            "path": [
              ""
            ],
            "host": [
              "cli",
              "requestcatcher",
              "com"
            ],
            "query": [
              {
                "key": "test",
                "value": null
              }
            ],
            "variable": []
          },
          "headers": [
            {
              "key": "User-Agent",
              "value": "PostmanRuntime/7.42.0",
              "system": true
            },
            {
              "key": "Accept",
              "value": "*/*",
              "system": true
            },
            {
              "key": "Cache-Control",
              "value": "no-cache",
              "system": true
            },
            {
              "key": "Postman-Token",
              "value": "c099574a-a9bd-4f05-b89a-df02e69f77c3",
              "system": true
            },
            {
              "key": "Host",
              "value": "cli.requestcatcher.com",
              "system": true
            },
            {
              "key": "Accept-Encoding",
              "value": "gzip, deflate, br",
              "system": true
            },
            {
              "key": "Connection",
              "value": "keep-alive",
              "system": true
            },
            {
              "key": "Content-Length",
              "value": "0",
              "system": true
            }
          ],
          "method": "POST"
        },
        "response": {
          "id": "89ce8c6e-7644-491a-b4e3-0faeedfd9718",
          "_details": {
            "name": "OK",
            "detail": "Standard response for successful HTTP requests. The actual response will depend on the request method used. In a GET request, the response will contain an entity corresponding to the requested resource. In a POST request the response will contain an entity describing or containing the result of the action.",
            "code": 200,
            "standardName": "OK"
          },
          "status": "OK",
          "code": 200,
          "headers": [
            {
              "key": "Accept-Ranges",
              "value": "bytes"
            },
            {
              "key": "Content-Length",
              "value": "1549"
            },
            {
              "key": "Content-Type",
              "value": "text/html; charset=utf-8"
            },
            {
              "key": "Last-Modified",
              "value": "Sat, 05 Oct 2024 14:46:41 GMT"
            },
            {
              "key": "Date",
              "value": "Sun, 15 Dec 2024 05:39:02 GMT"
            }
          ],
          "stream": {
            "type": "Buffer",
            "data": [
              60,
            <snip>
              62
            ]
          },
          "cookies": [],
          "responseTime": 449,
          "responseSize": 1549,
          "downloadedBytes": 1549
        },
        "tests": [],
        "errors": []
      }
    ],
    "runError": null
  }
}

<snip>の部分はレスポンスボディの値がストリームが記載されシンプルなテスト環境でも1000行以上になり冗長です。目的として戻りステータスコードやテスト結果が知りたいだけであれば(ほとんどの場合そうだと思います)--reporter-json-omitResponseBodiesを付与することで省略されます。

また-r junitと指定することで以下のようにJunit形式のXMLファイルが生成されます。

<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="New Collection" tests="1" time="0.556">
  <testsuite name="https://cli.requestcatcher.com/" id="d1a7ddea-a28d-49de-beda-488000054ca9" timestamp="2024-12-15T05:44:16.308Z" tests="0" failures="0" errors="0" time="0.556"/>
</testsuites>

環境変数 --verbose オプション

以下の通り、Response Bodyを含めた通常より多めの情報が出力されます。

Running your collection...
postman

New Collection

→ https://cli.requestcatcher.com/

  POST https://cli.requestcatcher.com/?test  
  200 OK ★ 565ms time ★ 259B↑ 1.74kB↓ size ★ 8↑ 5↓ headers ★ 0 cookies
  ┌ ↓ text/html ★ text ★ html ★ utf8 ★ 1.55kB
  │ <html><head><title>request catcher</title><meta content="dark light" name="color-scheme">  
  │ <link href="https://fonts.googleapis.com/css?family=Ubuntu+Mono|Source+Code+Pro|Cousine|O  
  │ pen+Sans:400,300,600" rel="stylesheet" type="text/css"><link rel="stylesheet" href="asset  
  │ s/static.da0603e4.css?v=0.2"><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.  
  │ 2.4/jquery.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js  
  │ /2.24.0/moment.min.js"></script><script type="text/javascript" src="assets/catcher.595c1f  
  │ 61.js"></script></head><body> <div id="page"> <div id="content"> <div id="header"> <h1>re  
  │ quest catcher</h1> </div> <div id="selector"></div> <div id="no-requests"> <div class="me  
  │ ssage">No requests have been received yet :(</div> <div class="try-it"> <span class="code  
  │ "> curl -X POST -d 'Hello World!' https://<span id="hostname">requestcatcher.com</span>/t  
  │ est </span> </div> </div> <div id="requests"></div> <div class="clear"></div> </div> <div  
  │  id="referral"><a href="https://m.do.co/c/fd00053f4fc6" target="_blank">Hosted on <strong  
  │ >DigitalOcean</strong>. Get a $200 credit and support Request Catcher.</a></div> </div> <  
  │ script>!function(e,a,t,n,g,c,o){e.GoogleAnalyticsObject=g,e.ga=e.ga||function(){(e.ga.q=e  
  │ .ga.q||[]).push(arguments)},e.ga.l=1*new Date,c=a.createElement(t),o=a.getElementsByTagNa  
  │ me(t)[0],c.async=1,c.src="//www.google-analytics.com/analytics.js",o.parentNode.insertBef  
  │ ore(c,o)}(window,document,"script",0,"ga"),ga("create","UA-55875288-1","auto"),ga("send",  
  │ "pageview");</script> </body></html>
  └
  prepare   wait   dns-lookup   tcp-handshake   ssl-handshake   transfer-start   download   process   total
  20ms      1ms    198ms        117ms           117ms           122ms            7ms        863µs     586ms


┌─────────────────────────┬─────────────────────┬────────────────────┐
│                         │            executed │             failed │
├─────────────────────────┼─────────────────────┼────────────────────┤
│              iterations │                   1 │                  0 │
├─────────────────────────┼─────────────────────┼────────────────────┤
│                requests │                   1 │                  0 │
├─────────────────────────┼─────────────────────┼────────────────────┤
│            test-scripts │                   0 │                  0 │
├─────────────────────────┼─────────────────────┼────────────────────┤
│      prerequest-scripts │                   0 │                  0 │
├─────────────────────────┼─────────────────────┼────────────────────┤
│              assertions │                   0 │                  0 │
├─────────────────────────┴─────────────────────┴────────────────────┤
│ total run duration: 693ms                                          │
├────────────────────────────────────────────────────────────────────┤
│ total data received: 1.55kB (approx)                               │
├────────────────────────────────────────────────────────────────────┤
│ average response time: 565ms [min: 565ms, max: 565ms, s.d.: 0µs]   │
├────────────────────────────────────────────────────────────────────┤
│ average DNS lookup time: 198ms [min: 198ms, max: 198ms, s.d.: 0µs] │
├────────────────────────────────────────────────────────────────────┤
│ average first byte time: 122ms [min: 122ms, max: 122ms, s.d.: 0µs] │
└────────────────────────────────────────────────────────────────────┘

Uploading Postman CLI run data to Postman Cloud...
�w�肳�ꂽ�p�X��������܂���B

Response received from postman run publish API
{"message":"Successfully imported collection run","postmanRunUrl":"https://go.postman.co/workspace/6612a719-6eba-4917-bd3c-b3b8191ff8ff/run/36690477-fe8800b9-d6b2-4449-b890-a95a05eb62d3","requestId":"d0f15c42-8701-46da-bb58-af298b8fe33c"}
Uploaded successfully! View on Postman: https://go.postman.co/workspace/6612a719-6eba-4917-bd3c-b3b8191ff8ff/run/36690477-fe8800b9-d6b2-4449-b890-a95a05eb62d3

Discussion