Knowing the script execution order helps you control when your test code runs in Postman. This makes your tests reliable and organized.
Script execution order 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
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
Postman
// Collection Pre-request Script
console.log('Collection Pre-request runs first');Postman
// Request Pre-request Script pm.variables.set('startTime', Date.now());
Postman
// Request Test Script pm.test('Status is 200', () => { pm.response.to.have.status(200); });
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');
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.
Practice
1. In Postman, which script runs first when you send a request?
Collection Pre-request Script, Folder Pre-request Script, or Request Pre-request Script?easy
Solution
Step 1: Understand script types in Postman
Pre-request scripts run before sending the request. They can be set at collection, folder, or request level.Step 2: Identify execution order of pre-request scripts
Postman runs pre-request scripts starting from the collection level, then folder, then request level.Final Answer:
Collection Pre-request Script -> Option AQuick Check:
Pre-request scripts run top-down: Collection first [OK]
Hint: Pre-request scripts run from collection to request level [OK]
Common Mistakes:
- Thinking request pre-request script runs first
- Confusing test scripts with pre-request scripts
- Assuming folder scripts run before collection scripts
2. Which of the following is the correct syntax to add a test script in Postman that checks if the response status is 200?
easy
Solution
Step 1: Recall Postman test script syntax
Postman uses pm.test() with a callback function to define tests.Step 2: Verify correct assertion method
pm.response.to.have.status(200) is the correct way to check status code 200.Final Answer:
pm.test('Status is 200', function () { pm.response.to.have.status(200); }); -> Option DQuick Check:
Use pm.test() and pm.response.to.have.status() [OK]
Hint: Use pm.test() with pm.response.to.have.status() [OK]
Common Mistakes:
- Using pm.check() which does not exist
- Using test() without pm namespace
- Using pm.assert() which is invalid
3. Given these scripts in Postman:
After sending the request, what is the value of
Collection Pre-request Script: sets variable var1 = 'A'Folder Pre-request Script: sets var1 = 'B'Request Pre-request Script: sets var1 = 'C'After sending the request, what is the value of
var1 in the test script?medium
Solution
Step 1: Understand script execution order for pre-request scripts
Pre-request scripts run in order: collection, then folder, then request. Each can overwrite variables.Step 2: Trace variable assignment
Collection sets var1='A', folder overwrites var1='B', request overwrites var1='C'. Final value is 'C'.Final Answer:
'C' -> Option AQuick Check:
Last pre-request script sets final variable [OK]
Hint: Last pre-request script overwrites variables [OK]
Common Mistakes:
- Assuming collection script value remains
- Thinking folder script overwrites request script
- Forgetting scripts run before request
4. You wrote a test script in Postman but it never runs after sending the request. Which is the most likely reason?
medium
Solution
Step 1: Understand when test scripts run
Test scripts run after the response is received, and must be placed in the Tests tab.Step 2: Identify script placement error
If the script is in Pre-request Script tab, it runs before sending request, not after response, so test code won't run as expected.Final Answer:
You placed the script in the Pre-request Script tab instead of Tests tab -> Option CQuick Check:
Tests run only in Tests tab after response [OK]
Hint: Put test code in Tests tab, not Pre-request tab [OK]
Common Mistakes:
- Confusing pre-request and test script tabs
- Assuming saving request affects script execution
- Thinking collection runner is needed for tests
5. You have a collection with a pre-request script setting a variable
token. A folder inside the collection has a pre-request script that updates token only if it is empty. The request inside the folder has a test script that checks if token is set. Which script execution order ensures the test script sees the updated token?hard
Solution
Step 1: Recall Postman script execution order
Pre-request scripts run in order: collection, folder, request. Test scripts run after response.Step 2: Understand variable update flow
Collection sets token, folder updates if empty, then request test script runs after response to check token.Step 3: Confirm correct order
This order ensures token is set and updated before test script checks it.Final Answer:
Collection pre-request script runs first, then folder pre-request script, then request test script -> Option BQuick Check:
Pre-request scripts run before tests in order [OK]
Hint: Pre-request scripts run before tests, top-down order [OK]
Common Mistakes:
- Thinking test scripts run before pre-request scripts
- Assuming folder scripts run before collection scripts
- Believing request pre-request scripts run after tests
