0
0
Postmantesting~8 mins

Reusable test scripts in Postman - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Reusable test scripts
Folder Structure
PostmanCollectionRoot/
├── collections/
│   ├── UserManagement.postman_collection.json
│   ├── ProductAPI.postman_collection.json
│   └── SharedScripts.postman_collection.json
├── environments/
│   ├── dev.postman_environment.json
│   ├── staging.postman_environment.json
│   └── prod.postman_environment.json
├── scripts/
│   ├── pre-request/
│   │   ├── authToken.js
│   │   └── setTimestamp.js
│   ├── tests/
│   │   ├── validateStatusCode.js
│   │   └── validateResponseTime.js
│   └── utils/
│       └── commonHelpers.js
└── README.md
Test Framework Layers
  • Collections: Group API requests logically (e.g., UserManagement, ProductAPI).
  • Environments: Store environment-specific variables like base URLs and credentials.
  • Reusable Scripts: Store common pre-request and test scripts separately for reuse across collections.
  • Utilities: Helper functions used by reusable scripts to avoid duplication.
  • Tests: Individual test scripts that assert API responses, imported into collections.
Configuration Patterns
  • Environment Variables: Use Postman environments to switch between dev, staging, and production easily.
  • Global Variables: Store tokens or values shared across collections.
  • Script Imports: Use pm.sendRequest or eval() carefully to include reusable scripts or use Postman's pm.variables to share data.
  • Parameterization: Use variables in URLs, headers, and bodies to make scripts flexible.
Test Reporting and CI/CD Integration
  • Newman CLI: Run Postman collections in CI pipelines and generate reports in formats like JSON, HTML.
  • CI/CD Tools: Integrate Newman runs in Jenkins, GitHub Actions, GitLab CI for automated testing on code changes.
  • Report Plugins: Use reporters like newman-reporter-html for readable test reports.
  • Fail Fast: Configure CI to stop on critical test failures to save resources.
Best Practices
  • Centralize Reusable Scripts: Keep common test and pre-request scripts in a dedicated folder or collection for easy maintenance.
  • Use Environment Variables: Avoid hardcoding values; use variables to adapt scripts to different environments.
  • Modularize Tests: Write small, focused test scripts that can be combined or reused.
  • Document Scripts: Add comments and README files explaining script purpose and usage.
  • Version Control: Store collections and scripts in Git or similar to track changes and collaborate.
Self Check

Where in this folder structure would you add a new reusable pre-request script for generating a unique request ID?

Key Result
Organize reusable Postman scripts in dedicated folders and collections to enable easy sharing and maintenance across API tests.