Skip to main content

Command Palette

Search for a command to run...

Testing with Postman: Looping Test

Updated
2 min read
Testing with Postman: Looping Test
B
I build automation frameworks — not just tests. Over 10 years I've designed and shipped production testing platforms across payment, insurance, and healthcare domains. My instinct is always toward architecture: clean abstractions, maintainable patterns, and systems that don't fall apart when the product scales. What I'm working on now: - A Claude/Playwright agent framework that generates test cases directly from Jira tickets - Concurrency and race-condition coverage for financial transactions - A modular Playwright/JavaScript platform covering UI and 20+ API service integrations with 100+ JSON schema validators I've built production automation in Playwright, Geb/Spock, Rest-Assured, and Selenium across payment, insurance, and healthcare domains. When a stack isn't working, I replace it. When a pattern is wrong, I fix it. If the automation problem is interesting, I'm probably already thinking about it.

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.

Setting Up a Looping 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")
}

Links

Postman Quick Reference Guide Chai Assertion Library Regex Cheat Sheet Postman: The Complete Guide on Udemy

More from this blog

B

Ben Weese - Senior QA Engineer

13 posts

As the son of a programmer I grew up on computers and have always loved them. Now I work as a Software Development Engineer in Test to write code and test things. I also enjoy my homelab hobby.