0
0
Postmantesting~8 mins

Environment variables in Postman - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Environment variables
Folder Structure for Postman Environment Variables
postman-project/
├── collections/
│   └── my-api-collection.json
├── environments/
│   ├── dev.postman_environment.json
│   ├── staging.postman_environment.json
│   └── prod.postman_environment.json
├── tests/
│   └── test-scripts.js
├── globals.postman_environment.json
└── README.md
  
Test Framework Layers in Postman with Environment Variables
  • Collections: Group of API requests to test different endpoints.
  • Environments: JSON files holding variables like base URLs, tokens, credentials for different setups (dev, staging, prod).
  • Globals: Variables accessible across all environments and collections.
  • Tests: Scripts written in JavaScript inside requests to validate responses using environment variables.
  • Utilities: Scripts or pre-request scripts to set or update environment variables dynamically.
Configuration Patterns for Environment Variables in Postman
  • Separate Environment Files: Maintain separate JSON files for each environment (dev, staging, prod) with relevant variables.
  • Use Variable Scopes: Use environment variables for environment-specific data and global variables for common data.
  • Secure Sensitive Data: Store sensitive info like API keys in environment variables, not hardcoded in requests.
  • Dynamic Variable Updates: Use pre-request or test scripts to update environment variables during test runs.
  • Environment Selection: Select the appropriate environment in Postman UI or via CLI (Newman) when running tests.
Test Reporting and CI/CD Integration
  • Newman CLI: Run Postman collections with selected environment files and generate reports in formats like JSON, HTML.
  • CI/CD Pipelines: Integrate Newman commands in pipelines (GitHub Actions, Jenkins) specifying environment files for automated testing.
  • Report Analysis: Use generated reports to track pass/fail status of API tests per environment.
  • Environment Variable Management: Use secure storage or secrets management in CI/CD to inject environment variables safely.
Best Practices for Using Environment Variables in Postman Frameworks
  1. Keep Environment Files Separate: Avoid mixing variables for different environments to prevent accidental misuse.
  2. Use Descriptive Variable Names: Make variable names clear and consistent for easy understanding.
  3. Never Hardcode Sensitive Data: Always use environment variables for tokens, passwords, and keys.
  4. Update Variables Dynamically: Use scripts to refresh tokens or update variables during test execution.
  5. Version Control Environment Files Carefully: Exclude sensitive environment files from public repos or use encrypted storage.
Self Check Question

Where in this folder structure would you add a new environment variable for the staging environment?

Key Result
Use separate environment JSON files to manage variables for different setups, enabling flexible and secure API testing in Postman.