0
0
Postmantesting~8 mins

Script execution order in Postman - Framework Patterns

Choose your learning style9 modes available
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.