Bird
Raised Fist0
Postmantesting~20 mins

Environment variables in Postman - Practice Problems & Coding Challenges

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

What is the main purpose of using environment variables in Postman?

ATo store values that can change between different environments like development, testing, and production
BTo write JavaScript code that runs before sending a request
CTo create collections of API requests for sharing
DTo generate random data for testing
Attempts:
2 left
💡 Hint

Think about how you can reuse the same request with different settings.

❓ Predict Output
intermediate
2:00remaining
Output of Accessing Environment Variable in Test Script

What will be the output of the following Postman test script if the environment variable userId is set to 12345?

Postman
const userId = pm.environment.get('userId');
console.log(`User ID is: ${userId}`);
pm.test('Check userId', () => {
    pm.expect(userId).to.eql('12345');
});
AUser ID is: 12345 (test passes)
BUser ID is: undefined (test fails)
CUser ID is: 12345 (test fails)
DSyntaxError due to incorrect pm.expect usage
Attempts:
2 left
💡 Hint

Check how pm.environment.get retrieves the variable and how the assertion compares values.

❓ locator
advanced
2:00remaining
Correct Locator for Environment Variable in Postman Pre-request Script

Which of the following lines correctly sets an environment variable named token to the value abc123 in a Postman pre-request script?

Apm.globals.set('token', 'abc123');
Bpm.variables.set('token', 'abc123');
Cpm.environment.set('token', 'abc123');
Dpm.request.set('token', 'abc123');
Attempts:
2 left
💡 Hint

Remember environment variables are set with a specific Postman API method.

❓ assertion
advanced
2:00remaining
Assertion to Verify Environment Variable Exists and Is Not Empty

Which assertion correctly verifies that the environment variable sessionId exists and is not an empty string in a Postman test script?

Postman
const sessionId = pm.environment.get('sessionId');
Apm.expect(sessionId).to.equal(null);
Bpm.expect(sessionId).to.exist.and.to.be.empty;
Cpm.expect(sessionId).to.be.undefined;
Dpm.expect(sessionId).to.be.a('string').and.not.empty;
Attempts:
2 left
💡 Hint

Check how to assert a string is not empty and exists.

🔧 Debug
expert
3:00remaining
Debugging Environment Variable Not Updating in Postman

You have this test script in Postman:

pm.environment.set('count', 1);
let count = pm.environment.get('count');
count++;
pm.environment.set('count', count);
console.log('Count is:', count);

After running the request multiple times, the count variable always logs 2. What is the most likely reason?

APostman does not allow updating environment variables in test scripts
BThe environment variable <code>count</code> is being reset to 1 every time before incrementing
CThe <code>count++</code> operation does not change the variable value
DThe console.log statement is incorrect and does not show updated value
Attempts:
2 left
💡 Hint

Look at the first line and how it affects the variable on each run.

Practice

(1/5)
1. What is the main purpose of environment variables in Postman?
easy
A. To store reusable values that can be used across multiple requests
B. To write test assertions for API responses
C. To create new API endpoints automatically
D. To generate random data for requests

Solution

  1. Step 1: Understand environment variables role

    Environment variables hold values like URLs or tokens that you reuse in many requests.
  2. Step 2: Compare with other options

    Writing tests or generating data are different features, not the main use of environment variables.
  3. Final Answer:

    To store reusable values that can be used across multiple requests -> Option A
  4. Quick Check:

    Environment variables = reusable values [OK]
Hint: Environment variables hold reusable data for requests [OK]
Common Mistakes:
  • Confusing environment variables with test scripts
  • Thinking environment variables create endpoints
  • Assuming they generate random data automatically
2. Which of the following is the correct way to set an environment variable named token to abc123 in a Postman test script?
easy
A. pm.environment.set('token', 'abc123');
B. pm.variables.set('token', 'abc123');
C. pm.environment.get('token', 'abc123');
D. pm.setEnvironment('token', 'abc123');

Solution

  1. Step 1: Identify correct method to set environment variable

    Postman uses pm.environment.set(key, value) to set environment variables.
  2. Step 2: Check other options for correctness

    pm.variables.set sets local variables, pm.environment.get is for getting values, and pm.setEnvironment is not a valid method.
  3. Final Answer:

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

    Set env var = pm.environment.set(key, value) [OK]
Hint: Use pm.environment.set() to assign environment variables [OK]
Common Mistakes:
  • Using pm.environment.get() to set variables
  • Confusing pm.variables.set() with environment variables
  • Using non-existent methods like pm.setEnvironment()
3. Given the following Postman test script, what will be the value of the environment variable userId after execution?
pm.environment.set('userId', 10);
pm.environment.set('userId', Number(pm.environment.get('userId')) + 5);
medium
A. Error
B. 10
C. undefined
D. 15

Solution

  1. Step 1: Analyze first line setting userId

    The first line sets userId to 10.
  2. Step 2: Analyze second line updating userId

    The second line gets current userId (10), adds 5, and sets it back, so userId becomes 15.
  3. Final Answer:

    15 -> Option D
  4. Quick Check:

    10 + 5 = 15 [OK]
Hint: Get then set adds values correctly [OK]
Common Mistakes:
  • Assuming variable stays 10 after second line
  • Thinking get returns undefined initially
  • Expecting a runtime error
4. You wrote this Postman script to remove an environment variable sessionId but it does not work:
pm.environment.delete('sessionId');

What is the correct way to delete an environment variable?
medium
A. pm.environment.remove('sessionId');
B. pm.environment.unset('sessionId');
C. pm.environment.clear('sessionId');
D. pm.environment.deleteVariable('sessionId');

Solution

  1. Step 1: Identify correct method to delete environment variable

    Postman uses pm.environment.unset(key) to remove environment variables.
  2. Step 2: Check why original code fails

    pm.environment.delete() is not a valid method, so it does not remove the variable.
  3. Final Answer:

    pm.environment.unset('sessionId'); -> Option B
  4. Quick Check:

    Delete env var = pm.environment.unset(key) [OK]
Hint: Use pm.environment.unset() to remove variables [OK]
Common Mistakes:
  • Using pm.environment.delete() which is invalid
  • Trying pm.environment.remove() or clear() which don't exist for single variables
  • Confusing with non-existent deleteVariable()
5. You want to run the same Postman collection on two different servers: dev and prod. Which is the best way to use environment variables to switch the base URL without changing the request code?
hard
A. Use global variables to store the URLs and change them before each run
B. Hardcode the server URL in each request and manually change it before running
C. Create two environments with a variable baseUrl set to each server URL, then select the environment before running
D. Write separate collections for dev and prod with different URLs

Solution

  1. Step 1: Understand environment variables for different setups

    Creating separate environments with baseUrl variables allows easy switching without code changes.
  2. Step 2: Compare other options

    Hardcoding or separate collections cause manual errors and duplication; global variables are less flexible for multiple setups.
  3. Final Answer:

    Create two environments with a variable baseUrl set to each server URL, then select the environment before running -> Option C
  4. Quick Check:

    Use environments to switch URLs easily [OK]
Hint: Use separate environments with baseUrl variables [OK]
Common Mistakes:
  • Hardcoding URLs in requests
  • Using global variables instead of environments
  • Creating duplicate collections for each server
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