Skip to main content

Using Environment Variables in Tests

You can include environment variables in your test files using the format ${ENV_VAR_NAME:-default_value}. Testpilot will automatically expand these variables when running your tests. The syntax works as follows:
  • ${ENV_VAR_NAME} - Uses the value of the environment variable
  • ${ENV_VAR_NAME:-default_value} - Uses the value of the environment variable if set, otherwise uses the default value

Example: Parameterizing Host URLs

A common use case is parameterizing the host URL in your tests. This lets you run the same tests against different environments without modifying your test files:
In this example:
  • If TEST_HOST is set (e.g., export TEST_HOST=https://dev.example.com), the test will use that value
  • If TEST_HOST is not set, it will fall back to the default value https://staging.example.com

Running Tests with Different Parameters

You can run the same test with different parameter values by changing the environment variables:

Parameterizing Other Test Values

You can parameterize any string value in your test files, not just URLs:

Use Cases for Parameterized Tests

Parameterizing your tests is useful for:
  1. Environment Switching: Run the same tests against dev, staging, and production
  2. Configuration Changes: Test with different feature flags or settings
  3. Test Data Variation: Use different test accounts or input data
  4. CI/CD Integration: Configure tests differently based on the build environment
  5. Local Development: Allow developers to point tests to their local instances

Example in CI/CD Pipeline

Here’s how you might use parameterized tests in a CI/CD pipeline:

Best Practices

When parameterizing your tests:
  1. Always provide defaults: Use the :-default_value syntax to ensure tests work even without environment variables set
  2. Document parameters: Include comments or context that explains what parameters are available
  3. Use descriptive names: Choose environment variable names that clearly indicate their purpose
  4. Group related parameters: Keep related parameters together (e.g., TEST_HOST, TEST_PORT, TEST_PROTOCOL)
By parameterizing your tests, you create more flexible and maintainable test suites that can easily adapt to different environments and configurations.