0
0

More than 1 year has passed since last update.

POSTMANで単純なAPIの繰り返し実行をする

Posted at

やりたいこと

とあるAPIのデータ作成で大量データが必要になったので自動化したかったので、それをPOSTMANで作れないかと模索中…

つまり、下記みたいなAPIがあり
POST /users/{{userId}}
userIdを
0001
0002
0003

000N
みたいな感じでN回叩きたいケースがあった。

実際はこんな感じで等間隔で実行されるイメージ
POST /users/0001
POST /users/0002
POST /users/0003

POST /users/000N

POSTMANのRunnerを使う

Collectionに登録

image.png

URL

http://localhost:3000/users/{{userId}}

Pre-request Scriptに下記を登録

初回の1回目

postman.setEnvironmentVariable("counter", 1);
console.log(ret)
pm.collectionVariables.set("userId", ret);

これはPOSTMANの環境変数に初期値を設定するために実行、ここでは通常通り、Sendを実行し、環境変数をセットする

初回の2回目以降

postman.setEnvironmentVariable("counter", num + 1); // ここで環境変数をインクリメントする
var num = Number(environment.counter);
var ret = ( '0000' + num ).slice( -4 );
pm.collectionVariables.set("userId", ret);

Runnerで実行

Iterations や Delayを設定し、Start Runを実行。
とりあえずこれで繰り返し実行は簡単にできる。
image.png

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