0
0
Postmantesting~8 mins

Global variables in Postman - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Global variables
Folder Structure of a Postman Test Project
  PostmanProject/
  ├── collections/
  │   └── MyAPICollection.postman_collection.json
  ├── environments/
  │   ├── dev.postman_environment.json
  │   ├── staging.postman_environment.json
  │   └── prod.postman_environment.json
  ├── globals/
  │   └── global_variables.postman_globals.json
  ├── scripts/
  │   ├── pre-request-scripts/
  │   │   └── set-global-vars.js
  │   └── test-scripts/
  │       └── validate-response.js
  └── README.md
  
Test Framework Layers in Postman
  • Collections: Group of API requests organized logically.
  • Environments: Store environment-specific variables (e.g., URLs, tokens).
  • Global Variables: Variables accessible across all collections and environments.
  • Pre-request Scripts: JavaScript code that runs before each request to set or update variables.
  • Test Scripts: JavaScript code that runs after each request to validate responses and update variables.
  • Utilities: Shared scripts or helper functions to reuse code.
Configuration Patterns for Global Variables

Global variables are defined in the globals folder as JSON files or directly in Postman UI. They store values accessible in any request or environment.

Use environment variables for environment-specific data, and global variables for data shared across all environments.

Example of setting a global variable in a pre-request script:

  pm.globals.set("authToken", "12345abcde");
  

Example of reading a global variable in a test script:

  const token = pm.globals.get("authToken");
  pm.test("Token is set", () => {
    pm.expect(token).to.not.be.undefined;
  });
  
Test Reporting and CI/CD Integration

Postman collections can be run via newman CLI tool in CI/CD pipelines.

Global variables can be loaded or updated before or after runs using scripts or environment files.

Reports generated by Newman can be in JSON, HTML, or JUnit formats for integration with CI tools like Jenkins, GitHub Actions, or GitLab.

Example Newman command using global variables file:

  newman run collections/MyAPICollection.postman_collection.json \
    --environment environments/dev.postman_environment.json \
    --globals globals/global_variables.postman_globals.json \
    --reporters cli,html
  
Best Practices for Using Global Variables in Postman
  • Limit global variables: Use them only for truly shared data to avoid confusion.
  • Prefer environment variables: For data that changes per environment (dev, staging, prod).
  • Use descriptive names: Name global variables clearly to avoid collisions and improve readability.
  • Manage secrets carefully: Avoid storing sensitive data in global variables; use secure vaults or environment variables with restricted access.
  • Update globals programmatically: Use pre-request or test scripts to keep global variables current during test runs.
Self Check

Where in this folder structure would you add a new global variable for an API authentication token?

Answer: In the globals/global_variables.postman_globals.json file or by setting it via a pre-request script in the scripts/pre-request-scripts/ folder.

Key Result
Use global variables in Postman to share data across collections and environments, managed via JSON files and scripts.