Skip to main content

Types of Variables

PostQode provides versatile variable support with various scopes to cater to diverse development, testing, and collaboration needs. In PostQode, scopes refer to the specific contexts in which your requests operate, and each variable scope serves distinct purposes depending on the task. These are the four types of Variables:

Local Variable

A local variable is confined to a single test case, ensuring that variables defined in one test case do not interfere with others, which promotes test isolation. Local variables offer a flexible and isolated way to manage data specific to individual tests.

Key Features

  • Test case isolation: Variables are confined to a single test case
  • Highest precedence: Override all other variable types when defined
  • Dynamic management: Can be created and managed within hooks
  • Independent execution: Ensures smooth and independent test execution

Creating Local Variables

Local variables can be created in scripts or pre-request scripts:

pq.variables.set("variableName", value);

Fetching Local Variables

To retrieve the value of a local variable:

pq.variables.get("variableName");

Using Local Variables in Hooks

Local variables can be created and managed within hooks using pq.variables.set in scripts or pre-request scripts. This allows you to initialize, modify, or clean up local variables before and after each test case.

Use Cases

  • Test case initialization and cleanup
  • Temporary data storage during test execution
  • Test-specific calculations and transformations
  • Isolated test state management

Global Variable

A global variable, accessible throughout your entire workspace, applies to all requests and scripts, ensuring it can be used for all project executions.

Key Features

  • Workspace-wide accessibility: Available across all projects and environments
  • Universal scope: Can be referenced in any request or script
  • Persistent storage: Values remain consistent across all executions
  • Cross-project compatibility: Shared across different projects within the workspace

Use Cases

  • API keys and authentication tokens
  • Common configuration values
  • Shared constants and settings
  • Universal endpoints or URLs

Environment Variable

A variable with a scope limited to a specific environment, which allows you to store and access the values relevant to the context of that environment, such as different API endpoints or configurations. It is accessible only to specific individuals who are assigned to that project.

Key Features

  • Environment-specific scope: Limited to a particular environment context
  • Controlled access: Only accessible to assigned project members
  • Context-aware: Values can differ between environments (dev, staging, prod)
  • Isolation: Changes in one environment don't affect others

Use Cases

  • Environment-specific API endpoints
  • Database connection strings for different environments
  • Feature flags and configuration toggles
  • Environment-specific credentials and secrets

Test Suite Variable

A test suite variable stores values relevant to its tests, enhancing flexibility and organization, and is accessible only to project members.

Key Features

  • Test-specific scope: Limited to the context of specific test suites
  • Enhanced organization: Keeps test data organized and isolated
  • Project member access: Available only to assigned project members
  • Test flexibility: Allows dynamic test data management

Use Cases

  • Test data and mock values
  • Test-specific configuration parameters
  • Expected results and validation data
  • Test environment setup values

Variable Scope Hierarchy

The variable scopes in PostQode follow a hierarchical structure:

  1. Local Variables - Highest precedence, confined to individual test cases
  2. Global Variables - Workspace-wide availability, available everywhere
  3. Environment Variables - Environment-specific, override global when in scope
  4. Test Suite Variables - Test-specific, override global and environment variables within test context

Understanding these variable types and their appropriate use cases will help you build more organized, secure, and maintainable projects in PostQode.