0
0
Postmantesting~8 mins

Variable syntax ({{variable}}) in Postman - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Variable syntax ({{variable}})
Folder Structure
PostmanCollectionProject/
├── collections/
│   └── MyAPICollection.json        # Main Postman collection with requests using {{variable}} syntax
├── environments/
│   ├── dev.postman_environment.json  # Environment variables for development
│   ├── staging.postman_environment.json  # Environment variables for staging
│   └── prod.postman_environment.json  # Environment variables for production
├── globals/
│   └── globals.postman_globals.json  # Global variables accessible in all environments
├── tests/
│   └── test-scripts/                # Optional folder for external test scripts if used
├── README.md                       # Project documentation
└── postman.config.json             # Optional config for Newman or CI integration
Test Framework Layers
  • Collections Layer: Contains API requests using {{variable}} syntax to reference variables dynamically.
  • Environment Layer: Holds environment-specific variables (e.g., base URLs, tokens) that replace {{variable}} placeholders during runtime.
  • Global Variables Layer: Variables accessible across all environments and collections for common values.
  • Pre-request & Test Scripts Layer: JavaScript code to set, update, or validate variables before or after requests.
  • Configuration Layer: Settings for running collections via Newman or CI tools, specifying environment files and variables.
Configuration Patterns
  • Environment Files: Use separate JSON files for each environment (dev, staging, prod) with variables like base_url, auth_token.
  • Variable Scopes: Use environment variables for environment-specific data, global variables for shared data, and collection variables for collection-specific data.
  • Variable Naming: Use clear, consistent names like api_key, user_id to avoid confusion.
  • Secure Sensitive Data: Store sensitive variables (passwords, tokens) in environment files excluded from version control or use Postman secrets management.
  • Dynamic Variable Setting: Use pre-request scripts to update variables dynamically before requests run.
Test Reporting and CI/CD Integration
  • Newman CLI: Run Postman collections with environment variables via Newman command line, e.g., newman run MyAPICollection.json -e dev.postman_environment.json.
  • CI/CD Pipelines: Integrate Newman runs in pipelines (GitHub Actions, Jenkins) passing environment files and capturing reports.
  • Report Formats: Generate HTML, JSON, or JUnit reports from Newman for easy analysis.
  • Variable Injection: Use CI secrets or environment variables to inject sensitive data at runtime, avoiding hardcoding.
Best Practices
  1. Use Environment Variables for Flexibility: Avoid hardcoding URLs or tokens; use {{variable}} syntax to switch environments easily.
  2. Keep Sensitive Data Secure: Never commit secrets to version control; use environment files or secret managers.
  3. Consistent Naming: Use clear and consistent variable names to avoid confusion and errors.
  4. Leverage Variable Scopes: Understand and use global, environment, and collection variables properly for better control.
  5. Update Variables Dynamically: Use pre-request scripts to set or modify variables during test runs for dynamic testing.
Self Check

Where in this folder structure would you add a new environment variable for the staging environment to store the API base URL?

Key Result
Use Postman's {{variable}} syntax with environment and global variables to create flexible, reusable API tests.