Bird
Raised Fist0
Postmantesting~20 mins

Variable scope and precedence in Postman - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Postman Variable Scope Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Variable Scope in Postman

In Postman, variables can exist at different scopes: Global, Collection, Environment, and Local (within a request). Which variable scope has the highest precedence when resolving a variable name during a request execution?

AGlobal variables
BEnvironment variables
CCollection variables
DLocal variables
Attempts:
2 left
💡 Hint

Think about which variable is closest to the request itself.

❓ Predict Output
intermediate
2:00remaining
Output of Variable Resolution in Postman Script

Given the following Postman pre-request script and variable scopes, what will be the value of pm.variables.get('token') during the request execution?

Global variable token = "global123"

Environment variable token = "env456"

Collection variable token = "col789"

Pre-request script:

Postman
pm.variables.set('token', 'local000');
console.log(pm.variables.get('token'));
A"local000"
B"env456"
C"col789"
D"global123"
Attempts:
2 left
💡 Hint

Variables set in the script override other scopes.

❓ assertion
advanced
2:00remaining
Correct Assertion for Variable Value in Postman Test

You want to assert that the variable userId used in your request is the environment variable value, not a local or global one. Which assertion correctly verifies this in the test script?

Apm.test('userId is environment variable', () => pm.expect(pm.variables.get('userId')).to.eql(pm.environment.get('userId')));
Bpm.test('userId is environment variable', () => pm.expect(pm.variables.get('userId')).to.eql(pm.global.get('userId')));
Cpm.test('userId is environment variable', () => pm.expect(pm.environment.get('userId')).to.eql(pm.variables.get('userId')));
Dpm.test('userId is environment variable', () => pm.expect(pm.environment.get('userId')).to.eql(pm.global.get('userId')));
Attempts:
2 left
💡 Hint

Remember that pm.variables.get() returns the variable value with precedence applied.

🔧 Debug
advanced
2:00remaining
Debugging Variable Precedence Issue in Postman

You have a collection variable apiKey set to "colKey" and an environment variable apiKey set to "envKey". Your request script uses pm.variables.get('apiKey') but always returns "colKey" instead of "envKey". What is the most likely reason?

ALocal variables override all, so a local variable named "apiKey" must exist with value "colKey".
BEnvironment variables have higher precedence than collection variables, so "envKey" should be returned but there's a bug.
CCollection variables have higher precedence than environment variables, so "colKey" is returned.
DGlobal variables override both collection and environment variables, causing "colKey" to appear.
Attempts:
2 left
💡 Hint

Check the Postman variable precedence order.

❓ framework
expert
3:00remaining
Order of Variable Resolution in Postman

Arrange the following Postman variable scopes in the order Postman uses to resolve a variable name, from highest precedence to lowest.

A1, 2, 3, 4
B4, 3, 2, 1
C3, 4, 2, 1
D2, 3, 4, 1
Attempts:
2 left
💡 Hint

Think about which variables are closest to the request execution context.

Practice

(1/5)
1. In Postman, which variable scope has the highest precedence when accessing a variable value?
easy
A. Global variables
B. Environment variables
C. Collection variables
D. Local variables

Solution

  1. Step 1: Understand Postman variable scopes

    Postman has multiple variable scopes: local, environment, collection, and global.
  2. Step 2: Identify precedence order

    Local variables override environment, collection, and global variables when accessing a variable.
  3. Final Answer:

    Local variables -> Option D
  4. Quick Check:

    Highest precedence = Local variables [OK]
Hint: Local variables always override others in Postman [OK]
Common Mistakes:
  • Confusing environment variables as highest
  • Thinking global variables override local
  • Ignoring local scope existence
2. Which of the following is the correct way to set a local variable in a Postman test script?
easy
A. pm.variables.set('varName', 'value');
B. pm.environment.set('varName', 'value');
C. pm.collectionVariables.set('varName', 'value');
D. pm.globals.set('varName', 'value');

Solution

  1. Step 1: Recall methods to set variables in Postman

    Local variables are set using pm.variables.set(). Environment, collection, and global variables use their respective methods.
  2. Step 2: Match method to local variable scope

    pm.variables.set() sets a local variable for the current request or script.
  3. Final Answer:

    pm.variables.set('varName', 'value'); -> Option A
  4. Quick Check:

    Local variable set method = pm.variables.set() [OK]
Hint: Use pm.variables.set() for local variables [OK]
Common Mistakes:
  • Using pm.environment.set() for local variables
  • Confusing collection and global variable setters
  • Forgetting to use pm.variables for local scope
3. Given these variables set in Postman:
pm.environment.set('token', 'envToken');
pm.collectionVariables.set('token', 'colToken');
pm.variables.set('token', 'localToken');
What will pm.variables.get('token') return?
medium
A. 'envToken'
B. 'localToken'
C. 'colToken'
D. null

Solution

  1. Step 1: Identify variable scopes and values

    Environment variable 'token' = 'envToken', collection variable 'token' = 'colToken', local variable 'token' = 'localToken'.
  2. Step 2: Understand variable precedence when getting value

    pm.variables.get() returns the local variable value if set, ignoring environment and collection variables.
  3. Final Answer:

    'localToken' -> Option B
  4. Quick Check:

    Local variable value returned by pm.variables.get() [OK]
Hint: pm.variables.get() returns local variable if exists [OK]
Common Mistakes:
  • Assuming environment variable is returned
  • Confusing collection variable with local
  • Expecting null if multiple scopes exist
4. You wrote this Postman test script:
pm.environment.set('user', 'Alice');
pm.variables.set('user', 'Bob');
console.log(pm.environment.get('user'));

But the console logs 'Alice' instead of 'Bob'. What is the issue?
medium
A. pm.environment.set() overwrites local variables
B. pm.variables.set() does not set local variables
C. pm.variables.get() should be used to get local variable
D. pm.environment.get() always reads environment variable ignoring local

Solution

  1. Step 1: Analyze variable setting and getting methods

    pm.environment.set() sets environment variable; pm.variables.set() sets local variable. But pm.environment.get() reads environment variable only.
  2. Step 2: Identify correct method to get local variable

    To get local variable, use pm.variables.get(), not pm.environment.get().
  3. Final Answer:

    pm.variables.get() should be used to get local variable -> Option C
  4. Quick Check:

    Use matching get method for variable scope [OK]
Hint: Use pm.variables.get() to read local variables [OK]
Common Mistakes:
  • Using pm.environment.get() to read local variables
  • Assuming set method affects all scopes
  • Expecting pm.variables.set() to overwrite environment
5. You have a global variable 'count' set to '5'. In a collection run, you set a collection variable 'count' to '10'. In a pre-request script, you set a local variable 'count' to '15'. Which value will be used if your test script calls pm.variables.get('count') and why?
hard
A. '15' because local variables have highest precedence
B. '5' because global variables have highest precedence
C. '10' because collection variables override local variables
D. null because variable names conflict

Solution

  1. Step 1: Review variable scopes and precedence

    Global variable 'count' = '5', collection variable 'count' = '10', local variable 'count' = '15'. Local variables override collection and global.
  2. Step 2: Determine which value pm.variables.get() returns

    pm.variables.get() returns the local variable value if it exists, ignoring collection and global variables.
  3. Final Answer:

    '15' because local variables have highest precedence -> Option A
  4. Quick Check:

    Local variable value returned by pm.variables.get() [OK]
Hint: Local variables override collection and global variables [OK]
Common Mistakes:
  • Thinking global variables override local
  • Assuming collection variables override local
  • Believing variable conflicts cause null
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