Run order and flow control help you decide which tests run first and how they connect. This makes your testing organized and efficient.
Run order and flow control in Postman
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Postman
pm.test('Test name', function () { // test code here }); // To control flow, use pm.setNextRequest('RequestName') to jump to a specific request // Example: pm.setNextRequest('Login'); // runs 'Login' request next // To stop running further requests: pm.setNextRequest(null);
pm.setNextRequest() controls which request runs next in a collection run.
Setting pm.setNextRequest(null) stops the run after the current request.
Examples
Postman
pm.test('Check status is 200', function () { pm.response.to.have.status(200); });
Postman
// Run 'GetUser' request next pm.setNextRequest('GetUser');
Postman
// Stop running any more requests pm.setNextRequest(null);
Sample Program
This example shows two requests: 'Login' and 'GetUserData'. After login, if it fails, the run stops. If login succeeds, it moves to 'GetUserData'. This controls the flow based on test results.
Postman
// Request 1: Login pm.test('Login successful', function () { pm.response.to.have.status(200); const jsonData = pm.response.json(); pm.environment.set('token', jsonData.token); }); // If login fails, stop running further requests if (pm.response.code !== 200) { pm.setNextRequest(null); } else { pm.setNextRequest('GetUserData'); } // Request 2: GetUserData // This request uses the token set in environment pm.test('User data received', function () { pm.response.to.have.status(200); pm.test('User name exists', function () { const jsonData = pm.response.json(); pm.expect(jsonData.name).to.exist; }); });
Important Notes
Use clear and unique request names to control flow easily.
Flow control helps save time by stopping tests early if needed.
Remember to reset flow control if you want to run all requests normally.
Summary
Run order controls which test runs first and next.
Use pm.setNextRequest() to jump to specific requests.
Use pm.setNextRequest(null) to stop running more tests.
Practice
1. In Postman, what does
pm.setNextRequest() do in a collection run?easy
Solution
Step 1: Understand the purpose of
This function controls the flow by specifying the next request to run in the collection.pm.setNextRequest()Step 2: Compare other options
Stopping the run is done bypm.setNextRequest(null), not this function. Restarting or logging are unrelated.Final Answer:
It sets which request runs next in the collection. -> Option AQuick Check:
Flow control = set next request [OK]
Hint: Remember: setNextRequest controls next request flow [OK]
Common Mistakes:
- Confusing pm.setNextRequest() with stopping the run
- Thinking it restarts the current request
- Assuming it logs data instead of controlling flow
2. Which of the following is the correct syntax to stop running any further requests in a Postman collection?
easy
Solution
Step 1: Identify the method to stop collection run
Postman usespm.setNextRequest(null)to stop running further requests.Step 2: Verify other options
Other options likepm.stopCollection()orpm.abortRun()do not exist in Postman scripting.Final Answer:
pm.setNextRequest(null) -> Option DQuick Check:
Stop run = setNextRequest(null) [OK]
Hint: Use null in setNextRequest to stop run [OK]
Common Mistakes:
- Using a string like 'stop' instead of null
- Assuming non-existent functions stop the run
- Confusing stopping with skipping requests
3. Consider this Postman test script inside a request named 'Request A':
What happens if the response code is 404?
if (pm.response.code === 200) {
pm.setNextRequest('Request B');
} else {
pm.setNextRequest(null);
}What happens if the response code is 404?
medium
Solution
Step 1: Analyze the condition for response code 404
Since 404 is not 200, the else block runspm.setNextRequest(null).Step 2: Understand effect of
This stops the collection run immediately after the current request.pm.setNextRequest(null)Final Answer:
The collection run stops after 'Request A'. -> Option CQuick Check:
404 triggers stop = setNextRequest(null) [OK]
Hint: If condition false, setNextRequest(null) stops run [OK]
Common Mistakes:
- Assuming it continues to next request regardless
- Thinking it restarts the collection
- Believing an error is thrown automatically
4. You wrote this test script in Postman:
What is the effect on the collection run flow?
pm.setNextRequest('Request C');
pm.setNextRequest(null);What is the effect on the collection run flow?
medium
Solution
Step 1: Understand order of pm.setNextRequest calls
Only the lastpm.setNextRequest()call takes effect in a single script execution.Step 2: Analyze the last call
This stops the collection run immediately, ignoring previous setNextRequest calls.pm.setNextRequest(null)Final Answer:
The run stops immediately; 'Request C' is skipped. -> Option BQuick Check:
Last setNextRequest call wins = null stops run [OK]
Hint: Last setNextRequest call controls flow [OK]
Common Mistakes:
- Thinking both calls run in sequence
- Assuming first call overrides last
- Believing it causes infinite loops
5. You want to run a Postman collection where:
- If 'Login' request succeeds (status 200), run 'GetData'.
- If 'Login' fails, stop the run.
- After 'GetData', always run 'Logout'.
Which sequence of
- If 'Login' request succeeds (status 200), run 'GetData'.
- If 'Login' fails, stop the run.
- After 'GetData', always run 'Logout'.
Which sequence of
pm.setNextRequest() calls in the 'Login' and 'GetData' test scripts achieves this flow?hard
Solution
Step 1: Control flow after 'Login'
If status is 200, next request should be 'GetData'; else stop run withpm.setNextRequest(null).Step 2: Control flow after 'GetData'
Always run 'Logout' next, sopm.setNextRequest('Logout')is set in 'GetData' tests.Final Answer:
In 'Login': pm.setNextRequest('GetData') if 200 else pm.setNextRequest(null); In 'GetData': pm.setNextRequest('Logout') -> Option AQuick Check:
Conditional jump + final logout = In 'Login':pm.setNextRequest('GetData')if 200 elsepm.setNextRequest(null);
In 'GetData':pm.setNextRequest('Logout')[OK]
Hint: Use conditional setNextRequest in Login, fixed next in GetData [OK]
Common Mistakes:
- Setting wrong next request after Login
- Not stopping run on Login failure
- Skipping Logout after GetData
