LoginSignup
30
19

More than 3 years have passed since last update.

CLI版PostmanのNewmanを使ってみた

Last updated at Posted at 2019-12-05

APIのテストでPostmanを使っていますがNewmanというコマンドラインで実行できるものがあったので使ってみました。

Postmanとは?

APIリクエストを簡単に実行できたり、開発で役に立つツールです。

公式サイト:https://www.getpostman.com/
ドキュメント:https://learning.getpostman.com/docs/postman/launching-postman/introduction/

Newmanとは?

NewmanはPostmanで作ったjsonを読み込んでコマンドラインから実行できるツールです。

ドキュメント:https://learning.getpostman.com/docs/postman/collection-runs/command-line-integration-with-newman/

インストール

npm install -g newman

準備

Postman Echoという確認用のAPIサービスがあるのでこちらで確認してみます。
Postman Learning CenterのページからCollection.jsonCSVを取得します。
CSVはそのまま使用すると実行結果が失敗となったため、valueの値を"で囲みました。

Collection.json
{
    "variables": [],
    "info": {
        "name": "Using data files",
        "_postman_id": "5d604721-fce3-a131-635c-fbbf5744a169",
        "description": "",
        "schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
    },
    "item": [
        {
            "name": "POST Request",
            "event": [
                {
                    "listen": "test",
                    "script": {
                        "type": "text/javascript",
                        "exec": [
                            "var jsonData = JSON.parse(responseBody);",
                            "",
                            "tests['Response has data value'] = jsonData.form.foo === data.value"
                        ]
                    }
                }
            ],
            "request": {
                "url": "postman-echo.com/{{path}}",
                "method": "POST",
                "header": [],
                "body": {
                    "mode": "formdata",
                    "formdata": [
                        {
                            "key": "foo",
                            "value": "{{value}}",
                            "type": "text",
                            "enabled": true
                        }
                    ]
                },
                "description": ""
            },
            "response": []
        }
    ]
}
request.csv
path,value
post,"1"
post,"2"
post,"3"
post,"4"

実行

基本的にはnewman run mycollection.jsonで実行します。
今回はリクエストをCSVファイルから読み込むので-dオプションを使用します。

$ newman run Collection.json -d requestData.csv

実行すると、

$  newman run Collection.json -d requestData.csv
newman

Using data files

Iteration 1/4

→ POST Request
  POST postman-echo.com/post [200 OK, 690B, 503ms]
  ✓  Response has data value

Iteration 2/4

→ POST Request
  POST postman-echo.com/post [200 OK, 785B, 173ms]
  ✓  Response has data value

Iteration 3/4

→ POST Request
  POST postman-echo.com/post [200 OK, 663B, 162ms]
  ✓  Response has data value

Iteration 4/4

→ POST Request
  POST postman-echo.com/post [200 OK, 784B, 169ms]
  ✓  Response has data value

┌─────────────────────────┬─────────────────────┬────────────────────┐
│                         │            executed │             failed │
├─────────────────────────┼─────────────────────┼────────────────────┤
│              iterations │                   4 │                  0 │
├─────────────────────────┼─────────────────────┼────────────────────┤
│                requests │                   4 │                  0 │
├─────────────────────────┼─────────────────────┼────────────────────┤
│            test-scripts │                   4 │                  0 │
├─────────────────────────┼─────────────────────┼────────────────────┤
│      prerequest-scripts │                   0 │                  0 │
├─────────────────────────┼─────────────────────┼────────────────────┤
│              assertions │                   4 │                  0 │
├─────────────────────────┴─────────────────────┴────────────────────┤
│ total run duration: 1208ms                                         │
├────────────────────────────────────────────────────────────────────┤
│ total data received: 1.5KB (approx)                                │
├────────────────────────────────────────────────────────────────────┤
│ average response time: 251ms [min: 162ms, max: 503ms, s.d.: 145ms] │
└────────────────────────────────────────────────────────────────────┘

成功しました。
実際に使うときは使用しているコレクションや環境変数をエクスポートして指定します。

-rオプションで実行結果をJSONで出力したり、
-eオプションで環境変数ファイルを指定できます。
オプションは他にも色々あるので確認してみてください。

ライブラリとしても使用できるようです。
Using Newman as a Library

newman.js
const 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!');
});

まとめ

gitフック1などに組み込んでおけば便利そうです。

参考

PostmanとNewmanを組み合わせて、CI/CDに組み込むREST APIの自動テストを作ろう!
newman
Postman Echo
Postman Learning Center


  1. gitフックを使ったことがないので今度使ってみたいと思います。 

30
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
19