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:- 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 valuehttps://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:- Environment Switching: Run the same tests against dev, staging, and production
- Configuration Changes: Test with different feature flags or settings
- Test Data Variation: Use different test accounts or input data
- CI/CD Integration: Configure tests differently based on the build environment
- 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:- Always provide defaults: Use the
:-default_value
syntax to ensure tests work even without environment variables set - Document parameters: Include comments or context that explains what parameters are available
- Use descriptive names: Choose environment variable names that clearly indicate their purpose
- Group related parameters: Keep related parameters together (e.g.,
TEST_HOST
,TEST_PORT
,TEST_PROTOCOL
)