Quantcast
Viewing all articles
Browse latest Browse all 470

Postman Collection Runnerの実行順序を動的に変えたりリトライする方法

WebAPIのテストに、Postmanを利用しています。Collection Runnerを使って、一連のAPIテストを流すのですが、テスト結果やEnvironmentの設定値によって、特定のAPIをリトライしたり、次のAPIをスキップしたり等、実行順序を動的に変更したくなったので、その方法を調べたメモ。 Collection Runnerの実行順序 通常のCollection Runnerの実行順序は、単純で上から下にシーケンシャルに実行されていきます。 APIをリトライ(繰り返し実行)する Testsを使って、繰り返し実行ができます。TestsはJavascriptで書きます。この例では、設定しておいたリトライ回数に達するまで15secおきにリトライします。APIのレスポンスが成功した場合は、その時点で次のAPI実行に移ります。 Tests pm.test("matche string TEST", function () { //initialize const responseJson = pm.response.json(); var count = pm.collectionVariables.get("retry_counter"); const count_limit = pm.collectionVariables.get("retry_count_limit"); if(responseJson.hoge.match("success") || count>=count_limit){ //clear retry count pm.collectionVariables.set("retry_counter", 0); } else{ //count up count++; pm.collectionVariables.set("retry_counter", count); console.log("リトライします:"+count+"回目"); //retry after 15sec setTimeout(function(){}, [15000]); var title = pm.info.requestName; postman.setNextRequest(title); } }); リトライ回数のカウント用変数、リトライの上限回数はCollection Variablesとして設定しておきます。Collection Variablesは、下記で設定できます。 APIをスキップする リトライと同じような方法で、テスト結果やenvironmentによってAPIをスキップすることができます。Testsの例では、environment variableの値によって次のAPIの実行是非を変えています。 Tests //検証環境では、TEST5までスキップする var stg_skip = pm.environment.get("stg_flag"); if(stg_skip == 1){ console.log("skip document"); postman.setNextRequest("TEST5") } 実行順序はこのようになります。 おわりに 実行順序の変更は、API単体で実行した場合は影響がありません。Collection Runnerで実行した場合のみ有効です。Testsのテストをする時は、Collection Runnerで試しましょう。 postman scripts 参考

Viewing all articles
Browse latest Browse all 470

Trending Articles