Run order and flow control help you decide which tests run first and how they connect. This makes your testing organized and efficient.
0
0
Run order and flow control in Postman
Introduction
When you want to run tests in a specific sequence, like login before fetching data.
When you need to stop running tests if one fails to save time.
When you want to run different tests based on previous test results.
When you want to run a group of tests automatically one after another.
When you want to control test execution in Postman collections.
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
This runs a simple test to check if the response status is 200 (OK).
Postman
pm.test('Check status is 200', function () { pm.response.to.have.status(200); });
This tells Postman to run the request named 'GetUser' next, regardless of the order in the collection.
Postman
// Run 'GetUser' request next pm.setNextRequest('GetUser');
This stops the collection run after the current request finishes.
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; }); });
OutputSuccess
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.