Testing with Postman: Looping Test

Search for a command to run...

No comments yet. Be the first to comment.
In this series I will cover testing with postman and using it for automation.
Postman uses the Chai Assertion Library so familiarize yourself with it. Using Chai Match Assertion You can define a format for your response using Regex and the match assertion. Below is an example of a data format for a mysql date. Date Format pm.t...
I have spent the last few weeks adding AI agents to my Playwright framework. Specifically Playwright's planner, generator, and healer, running locally through Claude Code. The Playwright agents explor

I decided to do a dump or a one stop shop for learning postman. You can go through my other post or just read this one. Testing Basics So to get started with postman you will need your API url and whether it is a GET, POST, PUT, DELETE or various oth...

Next is to setup a DevOps job you can go here to learn how to do that. For this section we will focus on the Execute shell and newman. With this I suggest going through Postman's official documentation found here. This takes your postman game and ups...

Schema is something you need to test to make sure when they need a string that it is indeed a string. If you google postman and schema checking you will also find a cool tool called tv4. Do not use this as it is out dated and no longer supported. Ple...

Another javascript library built into Postman is Lodash. #Looping Arrays with Lodash Sometimes you will want to iterate through an array. Maybe your response itself is an array. Luckily there is an easy for loop that can be used. This is use for wher...

In this blog we use setNextRequest to call to back to the same postman request. This allows us to reuse that request and change different variables for multiple test.
You can make a request loop back to itself and run again. You can also skip test and a bunch of other things using the same setNextRequest. However for now we will be using this code to reuse the same request. Do to the nature of SetNext request you will need a setup request. Below you will find the pre-request script that creates an array and stringifys it so that we don't get errors later when using it.
We then have a second array where we call shift. What that does is sets name to be the first name in the array and then removes the first element from the array.
So using the code below. Name becomes 'Billy-Bob' and the nameArray becomes ['Jo-Bob', 'Jim-Bob', 'Bob-Bob', 'Bobber-Ann'].
var nameArray = ['Billy-Bob', 'Jo-Bob', 'Jim-Bob', 'Bob-Bob', 'Bobber-Ann']
pm.environment.set("nameArray", JSON.stringify(nameArray))
pm.environment.set("name", nameArray.shift())
Continuing in the same request we move to the Test tab. Here we test the 1st element of the array and then if we have more array left we shift and set the array and name to the next in the array. We then move on to the next response in line.
//Run Test
var array = pm.environment.get("nameArray")
array = JSON.parse(array)
if(array.length > 0){
pm.environment.set("name", array.shift())
pm.environment.set("nameArray", JSON.stringify(array))
}
The next request we can run the same test. Then we again shift the array and assign the name. Then what we can do is set the next request to be the request we are in. If we didn't have the setup request then the pre-request would keep creating the new arrays and loop forever. Once the array has been shifted all the way through then we move on to the request following.
//Run Test
var array = pm.environment.get("nameArray")
array = JSON.parse(array)
if(array.length > 0){
pm.environment.set("name", array.shift())
pm.environment.set("nameArray", JSON.stringify(array))
postman.setNextRequest("NAME OF TEST")
}
Postman Quick Reference Guide Chai Assertion Library Regex Cheat Sheet Postman: The Complete Guide on Udemy