0
0
Postmantesting~8 mins

Local variables in Postman - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Local variables
Folder Structure of a Postman Test Project
PostmanProject/
├── collections/
│   ├── UserAPI.postman_collection.json
│   └── ProductAPI.postman_collection.json
├── environments/
│   ├── Development.postman_environment.json
│   └── Production.postman_environment.json
├── scripts/
│   ├── pre-request-scripts/
│   │   └── auth-setup.js
│   └── test-scripts/
│       └── response-validation.js
└── README.md

This structure organizes collections, environments, and reusable scripts.

Test Framework Layers in Postman
  • Collections: Group of API requests representing test scenarios.
  • Environments: Store variables like URLs, tokens for different setups.
  • Pre-request Scripts: JavaScript code run before requests to set up data or variables.
  • Test Scripts: JavaScript code run after requests to validate responses.
  • Local Variables: Variables defined within a single request or script, only available during that request execution.
  • Global & Environment Variables: Variables accessible across requests or environments.
Configuration Patterns for Local Variables in Postman
  • Use pm.variables.set('varName', value) to create or update a local variable within a request or script.
  • Local variables exist only during the execution of the current request and its scripts.
  • Local variables override environment and global variables with the same name during request execution.
  • Use pm.variables.get('varName') to access local variables.
  • Do not use local variables to store sensitive data across requests; use environment or global variables with proper security.
  • Keep local variables limited to data needed only temporarily for a single request or test step.
Test Reporting and CI/CD Integration
  • Postman collections can be run via Newman, the command-line runner.
  • Newman supports exporting detailed reports in formats like JSON, HTML, or JUnit XML.
  • Local variables are evaluated during each request run, so their values appear in the test execution logs.
  • Integrate Newman runs into CI/CD pipelines (e.g., Jenkins, GitHub Actions) to automate API testing.
  • Reports help verify that local variables were correctly set and used during tests.
Best Practices for Using Local Variables in Postman
  1. Limit Scope: Use local variables only when data is needed temporarily within a single request or script.
  2. Clear Naming: Name local variables clearly to avoid confusion with environment or global variables.
  3. Override Carefully: Remember local variables override environment/global variables during request execution.
  4. Debugging: Use console.log(pm.variables.get('varName')) in scripts to check local variable values.
  5. Do Not Persist Sensitive Data: Avoid storing secrets in local variables as they do not persist beyond the request.
Self Check

Where in this Postman framework structure would you add a local variable to store a temporary token needed only for one API request?

Key Result
Local variables in Postman are temporary variables scoped to a single request execution, set and accessed via pm.variables API.