Bird
Raised Fist0
Postmantesting~8 mins

Script execution order in Postman - Framework Patterns

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Framework Mode - Script execution order
Folder Structure of a Postman Test Project
  PostmanCollection/
  ├── collections/
  │   └── MyAPI.postman_collection.json
  ├── environments/
  │   ├── dev.postman_environment.json
  │   ├── staging.postman_environment.json
  │   └── prod.postman_environment.json
  ├── scripts/
  │   ├── pre-request-scripts/
  │   │   └── authSetup.js
  │   └── test-scripts/
  │       └── responseValidation.js
  ├── globals.json
  └── README.md
  
Test Framework Layers in Postman Script Execution
  • Pre-request Scripts Layer: Runs before the request is sent. Used to set variables, generate tokens, or prepare data.
  • Request Layer: The actual API call defined in the collection or folder.
  • Test Scripts Layer: Runs after the response is received. Used to validate response data, status codes, and set environment or global variables.
  • Collection and Folder Level Scripts: Pre-request and test scripts can be defined at collection or folder level and run in order with request-level scripts.
  • Environment and Global Variables Layer: Variables accessible across scripts to share data and state.
Configuration Patterns for Script Execution Order
  • Environment Variables: Define variables per environment (dev, staging, prod) to control script behavior.
  • Collection and Folder Scripts: Use collection-level pre-request and test scripts for common setup and teardown logic.
  • Request-level Scripts: Use for request-specific setup and validation.
  • Global Variables: Use sparingly for data shared across collections or runs.
  • Script Execution Order: Pre-request scripts run in this order: Collection > Folder > Request. Test scripts run in reverse order: Request > Folder > Collection.
Test Reporting and CI/CD Integration
  • Newman CLI: Run Postman collections in CI/CD pipelines with detailed JSON or HTML reports.
  • Reporters: Use built-in reporters like CLI, JSON, HTML, or third-party reporters for readable test results.
  • Fail Fast: Configure Newman to stop on first failure or continue to gather full test results.
  • Integration: Integrate with Jenkins, GitHub Actions, GitLab CI, or other pipelines to automate test runs on code changes.
  • Logging: Use console.log in scripts to output debug info visible in Newman or Postman console.
Best Practices for Script Execution Order in Postman
  • Use Collection and Folder Scripts for Common Logic: Avoid duplication by placing shared pre-request and test scripts at higher levels.
  • Keep Request Scripts Focused: Write request-level scripts only for validations or setup unique to that request.
  • Understand Execution Order: Pre-request scripts run Collection > Folder > Request; test scripts run Request > Folder > Collection.
  • Use Environment Variables to Control Flow: Set and read variables to manage dynamic data and conditional logic.
  • Log Wisely: Use console.log for debugging but clean logs before production runs.
Self Check Question

Where in this folder structure would you add a new pre-request script that sets an authentication token for all requests in the collection?

Key Result
Postman scripts execute in a clear order: pre-request scripts run from collection to request, test scripts run from request to collection, enabling organized setup and validation.

Practice

(1/5)
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
A. Collection Pre-request Script
B. Folder Pre-request Script
C. Request Pre-request Script
D. Test Script

Solution

  1. 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.
  2. Step 2: Identify execution order of pre-request scripts

    Postman runs pre-request scripts starting from the collection level, then folder, then request level.
  3. Final Answer:

    Collection Pre-request Script -> Option A
  4. Quick 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
A. test('Status is 200', () => response.status === 200);
B. pm.check('Status is 200', () => pm.response.status === 200);
C. pm.assert('Status is 200', pm.response.statusCode == 200);
D. pm.test('Status is 200', function () { pm.response.to.have.status(200); });

Solution

  1. Step 1: Recall Postman test script syntax

    Postman uses pm.test() with a callback function to define tests.
  2. Step 2: Verify correct assertion method

    pm.response.to.have.status(200) is the correct way to check status code 200.
  3. Final Answer:

    pm.test('Status is 200', function () { pm.response.to.have.status(200); }); -> Option D
  4. Quick 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:
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
A. 'C'
B. 'B'
C. Undefined
D. 'A'

Solution

  1. 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.
  2. Step 2: Trace variable assignment

    Collection sets var1='A', folder overwrites var1='B', request overwrites var1='C'. Final value is 'C'.
  3. Final Answer:

    'C' -> Option A
  4. Quick 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
A. You forgot to save the request
B. The request URL is invalid
C. You placed the script in the Pre-request Script tab instead of Tests tab
D. You did not enable the collection runner

Solution

  1. Step 1: Understand when test scripts run

    Test scripts run after the response is received, and must be placed in the Tests tab.
  2. 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.
  3. Final Answer:

    You placed the script in the Pre-request Script tab instead of Tests tab -> Option C
  4. Quick 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
A. Request test script runs before folder pre-request script
B. Collection pre-request script runs first, then folder pre-request script, then request test script
C. Folder pre-request script runs before collection pre-request script
D. Request pre-request script runs after test script

Solution

  1. Step 1: Recall Postman script execution order

    Pre-request scripts run in order: collection, folder, request. Test scripts run after response.
  2. Step 2: Understand variable update flow

    Collection sets token, folder updates if empty, then request test script runs after response to check token.
  3. Step 3: Confirm correct order

    This order ensures token is set and updated before test script checks it.
  4. Final Answer:

    Collection pre-request script runs first, then folder pre-request script, then request test script -> Option B
  5. Quick 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