Bird
Raised Fist0
Postmantesting~8 mins

Variable assignment in flows in Postman - Framework Patterns

Choose your learning style10 modes available
Framework Mode - Variable assignment in flows
Folder Structure
postman-project/
├── collections/
│   └── my-api-collection.json       # Postman collection with requests and scripts
├── environments/
│   ├── dev.postman_environment.json # Environment variables for Dev
│   ├── qa.postman_environment.json  # Environment variables for QA
│   └── prod.postman_environment.json # Environment variables for Prod
├── globals/
│   └── globals.postman_globals.json # Global variables
├── tests/
│   └── scripts/                     # Custom JS scripts for variable assignment and helpers
├── README.md
└── postman.config.json              # Optional config for Postman CLI or Newman
Test Framework Layers
  • Collections: Define API requests and flows. Each request can have pre-request and test scripts to assign variables.
  • Environments: Store environment-specific variables (e.g., base URLs, tokens). Variables can be updated dynamically during flows.
  • Globals: Variables accessible across all collections and environments for sharing data.
  • Scripts: JavaScript code in pre-request or test scripts to assign, update, or clear variables using Postman API (pm.variables, pm.environment, pm.globals).
  • Tests: Assertions and variable assignments happen here to validate responses and set variables for next requests.
Configuration Patterns
  • Environment Files: Use separate environment JSON files for Dev, QA, Prod to manage variables like URLs and credentials.
  • Variable Scopes: Use local (request-level), environment, and global scopes carefully to control variable visibility and lifetime.
  • Dynamic Variable Assignment: Assign variables in test scripts using pm.environment.set('varName', value) or pm.variables.set('varName', value) to pass data between requests.
  • Secrets Management: Store sensitive data in environment variables and avoid hardcoding in scripts or collections.
  • Version Control: Keep collections and environment files under version control (e.g., Git) for collaboration and history.
Test Reporting and CI/CD Integration
  • Newman CLI: Run Postman collections in CI pipelines using Newman. Supports JSON and HTML reports.
  • Reporters: Use built-in reporters (CLI, JSON, HTML) or third-party reporters for detailed test results.
  • CI/CD Integration: Integrate Newman runs in Jenkins, GitHub Actions, GitLab CI to automate tests on code changes.
  • Variable Injection: Pass environment variables dynamically in CI to run tests against different environments.
  • Failure Alerts: Configure notifications on test failures to keep the team informed.
Best Practices
  • Use Environment Variables for Dynamic Data: Assign variables in test scripts to pass data between requests instead of hardcoding.
  • Clear Variables When Not Needed: Remove or reset variables to avoid stale data affecting later tests.
  • Keep Scripts Simple and Readable: Write clear JavaScript in pre-request and test scripts for variable assignment.
  • Use Descriptive Variable Names: Helps understand what data is stored and passed in flows.
  • Test Variable Assignments Independently: Validate that variables are assigned correctly before using them in subsequent requests.
Self Check

Where in this folder structure would you add a new script to assign a variable after receiving an authentication token?

Key Result
Use environment and global variables with scripts in Postman collections to assign and manage variables dynamically in API test flows.

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