Bird
Raised Fist0
Postmantesting~8 mins

Local variables in Postman - Framework Patterns

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of local variables in Postman?
easy
A. To create global environment settings
B. To save data permanently across all requests
C. To share data between different collections
D. To store data only during a single request execution

Solution

  1. Step 1: Understand local variable scope

    Local variables exist only during the execution of one request and are not saved beyond that.
  2. Step 2: Compare with other variable types

    Global or environment variables persist beyond one request, unlike local variables.
  3. Final Answer:

    To store data only during a single request execution -> Option D
  4. Quick Check:

    Local variables = single request scope [OK]
Hint: Local variables last only during one request [OK]
Common Mistakes:
  • Confusing local variables with global variables
  • Thinking local variables persist across requests
  • Assuming local variables share data between collections
2. Which of the following is the correct syntax to set a local variable named token with value abc123 in Postman?
easy
A. pm.local.set('token', 'abc123');
B. pm.environment.set('token', 'abc123');
C. pm.variables.set('token', 'abc123');
D. pm.globals.set('token', 'abc123');

Solution

  1. Step 1: Identify the correct method for local variables

    Local variables use pm.variables.set to assign values.
  2. Step 2: Check other options for correctness

    pm.environment.set and pm.globals.set set environment and global variables, not local. pm.local.set is invalid.
  3. Final Answer:

    pm.variables.set('token', 'abc123'); -> Option C
  4. Quick Check:

    Set local variable = pm.variables.set [OK]
Hint: Use pm.variables.set() for local variables [OK]
Common Mistakes:
  • Using pm.environment.set instead of pm.variables.set
  • Trying to use pm.local.set which does not exist
  • Confusing global and local variable methods
3. Consider this Postman test script:
pm.variables.set('count', 5);
pm.variables.set('count', pm.variables.get('count') + 3);
console.log(pm.variables.get('count'));

What will be printed in the console?
medium
A. 8
B. 5
C. Error: Cannot add number to undefined
D. null

Solution

  1. Step 1: Set initial local variable 'count'

    The first line sets 'count' to 5.
  2. Step 2: Increment 'count' by 3

    The second line gets 'count' (5), adds 3, and sets it back to 8.
  3. Step 3: Output the updated value

    The console.log prints the current value of 'count', which is 8.
  4. Final Answer:

    8 -> Option A
  5. Quick Check:

    5 + 3 = 8 [OK]
Hint: Get then set to update local variable value [OK]
Common Mistakes:
  • Assuming the variable does not update
  • Expecting an error due to type mismatch
  • Confusing local with environment variables
4. You wrote this Postman script:
pm.variables.set('user', 'Alice');
console.log(pm.variables.get('username'));

What is the problem here?
medium
A. Variable 'username' was never set, so get returns undefined
B. Syntax error in pm.variables.set method
C. Local variables cannot be logged to console
D. pm.variables.get requires two arguments

Solution

  1. Step 1: Check variable names used

    The script sets 'user' but tries to get 'username', which was never set.
  2. Step 2: Understand get behavior for unset variables

    Getting an unset local variable returns undefined, no error occurs.
  3. Final Answer:

    Variable 'username' was never set, so get returns undefined -> Option A
  4. Quick Check:

    Unset variable get = undefined [OK]
Hint: Check variable names carefully for typos [OK]
Common Mistakes:
  • Assuming syntax error when variable name mismatches
  • Thinking pm.variables.get needs two arguments
  • Believing console.log cannot show local variables
5. You want to store a temporary token during a request and use it in multiple tests within that request. Which approach correctly uses local variables to achieve this?
hard
A. Set the token with pm.environment.set('token', 'abc') and get it with pm.variables.get('token')
B. Set the token once with pm.variables.set('token', 'abc') and get it in tests with pm.variables.get('token')
C. Set the token with pm.variables.set('token', 'abc') but cannot access it in other tests
D. Set the token with pm.globals.set('token', 'abc') and get it with pm.variables.get('token')

Solution

  1. Step 1: Understand local variable scope within one request

    Local variables set with pm.variables.set are accessible throughout the request execution, including multiple tests.
  2. Step 2: Verify correct get method usage

    Use pm.variables.get to retrieve the local variable value within the same request.
  3. Step 3: Check other options for scope mismatch

    Environment and global variables persist beyond one request and are not local. Also, local variables are accessible across tests in the same request.
  4. Final Answer:

    Set the token once with pm.variables.set('token', 'abc') and get it in tests with pm.variables.get('token') -> Option B
  5. Quick Check:

    Local variable set/get works within one request [OK]
Hint: Use pm.variables.set/get to share data within one request [OK]
Common Mistakes:
  • Using environment or global variables instead of local
  • Thinking local variables cannot be shared between tests in one request
  • Mixing set and get methods from different scopes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard