Bird
Raised Fist0
Postmantesting~20 mins

Variable assignment in flows in Postman - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Postman Variable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Variable assignment in Postman pre-request script
What will be the value of the environment variable token after running this pre-request script in Postman?
Postman
pm.environment.set('token', 'initial');
if (pm.response && pm.response.code === 200) {
  pm.environment.set('token', 'updated');
} else {
  pm.environment.set('token', 'failed');
}
A"failed"
B"initial"
C"updated"
Dundefined
Attempts:
2 left
💡 Hint
Remember that pre-request scripts run before the request is sent, so pm.response is not yet available.
❓ assertion
intermediate
2:00remaining
Correct assertion for variable assignment in Postman test script
Which assertion correctly verifies that the global variable userId has been assigned the value 12345 in a Postman test script?
Apm.test('userId is 12345', () => pm.globals.get('userId') === '12345');
Bpm.test('userId is 12345', () => pm.expect(pm.globals.get('userId')).to.eql('12345'));
Cpm.test('userId is 12345', () => pm.globals.userId === 12345);
Dpm.test('userId is 12345', () => pm.expect(pm.globals.userId).to.equal(12345));
Attempts:
2 left
💡 Hint
Use pm.expect with pm.globals.get() to access global variables properly.
🔧 Debug
advanced
2:00remaining
Debugging variable assignment in Postman flow
Given this Postman test script, why does the variable sessionId remain undefined after the request?
Postman
if (pm.response.code === 200) {
  pm.variables.set('sessionId', pm.response.json().session);
}
console.log(pm.variables.get('sessionId'));
ABecause <code>pm.variables.set</code> requires a third argument to persist the variable.
BBecause <code>pm.response.code</code> is undefined in test scripts.
CBecause <code>pm.response.json()</code> is not a valid method to parse JSON response.
DBecause <code>pm.variables</code> only stores variables for the current request and is not persisted between requests.
Attempts:
2 left
💡 Hint
Consider the scope and lifetime of pm.variables compared to environment or global variables.
🧠 Conceptual
advanced
2:00remaining
Understanding variable scopes in Postman flows
Which statement correctly describes the difference between pm.variables, pm.environment, and pm.globals in Postman?
A<code>pm.variables</code> are temporary for the current request, <code>pm.environment</code> variables persist per environment, and <code>pm.globals</code> persist globally across all environments.
B<code>pm.variables</code> persist globally, <code>pm.environment</code> are temporary, and <code>pm.globals</code> persist per environment.
C<code>pm.variables</code> and <code>pm.environment</code> are the same, but <code>pm.globals</code> are temporary.
D<code>pm.variables</code> persist per environment, <code>pm.environment</code> persist globally, and <code>pm.globals</code> are temporary.
Attempts:
2 left
💡 Hint
Think about how long each variable type lasts and where it is accessible.
❓ framework
expert
3:00remaining
Postman flow variable assignment and test execution order
In a Postman collection run, which sequence correctly describes when variables are assigned and accessible during the flow?
AVariables set in pre-request scripts are only available after the entire collection run finishes; test scripts run before pre-request scripts.
BTest scripts run before the request; then pre-request scripts run; then the request is sent; variables set in test scripts are not saved.
CPre-request scripts run first and can set variables; then the request is sent; then test scripts run and can update variables; environment and global variables persist between requests.
DRequests run first; then pre-request scripts run; then test scripts run; variables set in pre-request scripts are not accessible in test scripts.
Attempts:
2 left
💡 Hint
Recall the order of execution in Postman: pre-request script, request, test script.

Practice

(1/5)
1. What does pm.variables.set('token', 'abc123') do in a Postman test script?
easy
A. It saves the value 'abc123' to a variable named 'token' for later use.
B. It retrieves the value of the variable 'token'.
C. It deletes the variable named 'token'.
D. It sends 'abc123' as a request header named 'token'.

Solution

  1. Step 1: Understand the method pm.variables.set

    This method assigns a value to a variable in Postman scripts.
  2. Step 2: Analyze the parameters

    The first parameter is the variable name 'token', the second is the value 'abc123' to store.
  3. Final Answer:

    It saves the value 'abc123' to a variable named 'token' for later use. -> Option A
  4. Quick Check:

    pm.variables.set saves data [OK]
Hint: Set stores data; get retrieves it [OK]
Common Mistakes:
  • Confusing set with get method
  • Thinking set deletes variables
  • Assuming set sends data in requests
2. Which of the following is the correct syntax to assign a variable named 'userId' with value 42 in a Postman test script?
easy
A. pm.variables.get('userId', 42);
B. pm.variables.store('userId', 42);
C. pm.variables.assign('userId' = 42);
D. pm.variables.set('userId', 42);

Solution

  1. Step 1: Recall the correct method for assigning variables

    The method to assign a variable is pm.variables.set(name, value).
  2. Step 2: Check each option's syntax

    pm.variables.set('userId', 42); uses pm.variables.set('userId', 42); which is correct syntax.
  3. Final Answer:

    pm.variables.set('userId', 42); -> Option D
  4. Quick Check:

    Use pm.variables.set to assign [OK]
Hint: Set method assigns variables with (name, value) [OK]
Common Mistakes:
  • Using get instead of set to assign
  • Incorrect method names like assign or store
  • Wrong parameter syntax with equals sign
3. Consider this Postman test script snippet:
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. 5
B. 8
C. Error
D. undefined

Solution

  1. Step 1: Analyze the first assignment

    The variable 'count' is set to 5 initially.
  2. Step 2: Analyze the second assignment

    It gets the current 'count' value (5), adds 3, and sets 'count' to 8.
  3. Step 3: Check the console.log output

    It prints the updated value of 'count', which is 8.
  4. Final Answer:

    8 -> Option B
  5. Quick Check:

    5 + 3 = 8 [OK]
Hint: Get current value, add, then set updated value [OK]
Common Mistakes:
  • Assuming the first value stays unchanged
  • Thinking get returns undefined
  • Confusing set and get usage
4. You wrote this Postman test script:
pm.variables.set('status', 'active')
pm.variables.get('status')

But the test fails because the variable 'status' is undefined. What is the likely cause?
medium
A. Variables set in one request are not available in another without environment or global scope.
B. pm.variables.get returns undefined if set is not awaited.
C. Missing semicolon after set statement causes failure.
D. Variable names cannot be strings.

Solution

  1. Step 1: Understand variable scopes in Postman

    Variables set with pm.variables.set are local to the current request script and do not persist across requests.
  2. Step 2: Identify why 'status' is undefined

    If you try to get 'status' in a different request without using environment or global variables, it will be undefined.
  3. Final Answer:

    Variables set in one request are not available in another without environment or global scope. -> Option A
  4. Quick Check:

    Local variables don't persist across requests [OK]
Hint: Use environment/global variables for cross-request data [OK]
Common Mistakes:
  • Thinking semicolons affect variable persistence
  • Believing get waits for set automatically
  • Assuming variable names can't be strings
5. You want to save a user token from a response JSON and reuse it in the next request. Which flow correctly assigns and retrieves the token using Postman variables?
hard
A. In first request test script: pm.variables.set('token', pm.response.json().token);
In next request pre-request script: const token = pm.environment.get('token');
B. In first request test script: pm.variables.set('token', pm.response.json().token);
In next request pre-request script: const token = pm.variables.get('token');
C. In first request test script: pm.environment.set('token', pm.response.json().token);
In next request pre-request script: const token = pm.environment.get('token');
D. In first request test script: pm.environment.set('token', pm.response.json().token);
In next request pre-request script: const token = pm.variables.get('token');

Solution

  1. Step 1: Identify variable scope for cross-request use

    To reuse data across requests, environment variables must be used, not local variables.
  2. Step 2: Match correct set and get methods

    In first request test script: pm.environment.set('token', pm.response.json().token);
    In next request pre-request script: const token = pm.environment.get('token'); uses pm.environment.set and pm.environment.get, which correctly share data between requests.
  3. Final Answer:

    Use environment variables to save and retrieve token across requests. -> Option C
  4. Quick Check:

    Environment variables persist across requests [OK]
Hint: Use environment variables for data between requests [OK]
Common Mistakes:
  • Using pm.variables for cross-request data
  • Mixing environment and local variable methods
  • Not retrieving variables in the correct scope
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