0
0
Postmantesting~5 mins

Script execution order in Postman

Choose your learning style9 modes available
Introduction

Knowing the script execution order helps you control when your test code runs in Postman. This makes your tests reliable and organized.

You want to set up data before sending a request.
You need to check the response after a request runs.
You want to clean up or reset variables after all tests finish.
You want to run code before every request in a collection.
You want to run code after every request in a collection.
Syntax
Postman
Pre-request Script: Runs before the request is sent.
Test Script: Runs after the response is received.
Collection Pre-request Script: Runs before every request in the collection.
Collection Test Script: Runs after every request in the collection.
Folder Pre-request Script: Runs before every request in that folder.
Folder Test Script: Runs after every request in that folder.

Scripts run in this order: Collection Pre-request, Folder Pre-request, Request Pre-request, Request, Request Test, Folder Test, Collection Test.

This order helps you organize setup and checks at different levels.

Examples
This script runs before every request in the whole collection.
Postman
// Collection Pre-request Script
console.log('Collection Pre-request runs first');
This runs before the specific request to set a start time.
Postman
// Request Pre-request Script
pm.variables.set('startTime', Date.now());
This runs after the request to check if the response status is 200.
Postman
// Request Test Script
pm.test('Status is 200', () => {
    pm.response.to.have.status(200);
});
This script runs after every request in the collection finishes.
Postman
// Collection Test Script
console.log('Collection Test runs last');
Sample Program

This example shows the order of console logs matching the script execution order in Postman.

Postman
// Collection Pre-request Script
console.log('Start Collection Pre-request');

// Folder Pre-request Script
console.log('Start Folder Pre-request');

// Request Pre-request Script
console.log('Start Request Pre-request');

// Request
console.log('Sending Request');

// Request Test Script
pm.test('Response status is 200', () => {
    pm.response.to.have.status(200);
});
console.log('End Request Test');

// Folder Test Script
console.log('End Folder Test');

// Collection Test Script
console.log('End Collection Test');
OutputSuccess
Important Notes

Use console.log to see the order scripts run in Postman Console.

Collection and folder scripts help avoid repeating code in every request.

Remember test scripts run only after the response is received.

Summary

Scripts run in a clear order: collection, folder, request.

Pre-request scripts run before sending requests.

Test scripts run after receiving responses.