Skip to content

Instantly share code, notes, and snippets.

@yokawasa
Last active December 8, 2024 22:09
Show Gist options
  • Save yokawasa/d1523fc5aa11be1514f798ba0a6d97ea to your computer and use it in GitHub Desktop.
Save yokawasa/d1523fc5aa11be1514f798ba0a6d97ea to your computer and use it in GitHub Desktop.
Run all Postman collections in a specific workspace using Postman CLI / あるワークスペースにあるすべてのコレクションをPostman CLIで実行

実行手順

(1) ワークスペースのIDを取得 ワークスペースを開き、「⋯」メニューをクリックして、「Workspace info」をクリックしてください。これでワークスペースIDが表示されます

Screenshot 2024-11-12 at 15 24 58

(2) Postman API Keyを取得 下記ページを参考にAPI Keyを取得してください https://learning.postman.com/docs/developer/postman-api/authentication/

(3) Postman CLIをインストール

下記ページを参考にPostman CLIをインストールしてください https://learning.postman.com/docs/postman-cli/postman-cli-installation/

(4) 1と2で取得したワークスペースIDとPostman API Keyをそれぞれ、上記スクリプトの変数値として設定

POSTMAN_WORKSPACE_ID="<ワークスペースID>"
POSTMAN_API_KEY="<POSTMAN API KEY>"

(5) 実行

run-all-collections.sh スクリプト(file) にexec権限を付与して、スクリプト実行します

chmod +x run-all-collection.sh
./run-all-collection.sh

無事実行されると次のような内容がターミナルに出力されます。また、どういつディレクトリに各コレクション実行の出力内容がcollection-run-<collection-uid>.txt という名前のファイルに保存されます。

Screenshot 2024-11-12 at 15 33 31

もし出力内容のファイル保存が不要であれば、スクリプトのL20とL23をコメントアウトしてください。こんな感じで

#  {
    # Postman CLIでコレクションを実行
    postman collection run ${collectionUid} --verbose
#  } 2>&1 | tee collection-run-${collectionUid}.txt

REFERENCE

#!/bin/bash
# set -e -x
POSTMAN_WORKSPACE_ID="<WORKSPACE ID>"
POSTMAN_API_KEY="<POSTMAN API KEY>"
# Postman API経由でワークスペースのコレクション一覧を取得(コレクションuidを取得)
# API reference: https://documenter.getpostman.com/view/12959542/UV5XjJV8#3a56b6f8-8d0c-410f-a933-03e26589c742
collectionUidList=$(curl -s --location "https://api.getpostman.com/workspaces/${POSTMAN_WORKSPACE_ID}" \
--header "X-API-Key: ${POSTMAN_API_KEY}"| jq -r ".workspace.collections[].uid" )
# Postman CLIでPostmanにログイン
echo "login"
postman login --with-api-key ${POSTMAN_API_KEY}
for collectionUid in ${collectionUidList};
do
echo "Start running colleciton: ${collectionUid}"
{
# Postman CLIでコレクションを実行
postman collection run ${collectionUid} --verbose
} 2>&1 | tee collection-run-${collectionUid}.txt
echo "Collection run finished: ${collectionUid}"
done
@yokawasa
Copy link
Author

yokawasa commented Nov 12, 2024

Postman APIのjson出力から、コレクション一覧を取得するのに jqコマンドをつかってます( code )。jqコマンドのインストール方法は下記ページが参考になります

https://qiita.com/ponsuke0531/items/aa405a0f897cd0a78a8c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment