What is .env.test.local and how to use it
Learn what a .env.test.local file does, when to use it, and how to safely configure test-specific environment variables for local development and testing.
What is .env.test.local?
A .env.test.local file is an environment variable file intended to override base settings when you run tests locally. It acts as a local override so you can configure test behavior (such as database connections, API endpoints, and feature flags) without touching the shared, default configuration.
In many setups, the runtime loads environment variables from a chain of files in this order: .env, .env.local, .env.test, and .env.test.local. Each layer can override values set by the previous one, with the intent that local and test-specific values stay separate from your shared configuration. The exact order and file names depend on your framework and tooling, so check your project’s docs.
Contents are typically simple key=value pairs or framework-specific formats that your application reads at startup.
Example contents
# Example of test-specific overrides
APP_ENV=test
APP_DEBUG=false
DB_CONNECTION=sqlite
DB_DATABASE=:memory:
CACHE_DRIVER=array
MAIL_DRIVER=array
When to use .env.test.local
- When you want test runs to use a different database, endpoints, or feature flags without changing the default environment.
- When local developers need to isolate tests from their own development data or configurations.
- When your CI pipeline provides its own environment, but you still want to run tests locally with a distinct setup.
Note: Some projects rely on CI or test runners to inject values directly (via secrets managers or environment variables). In those cases, .env.test.local is optional but convenient for local experimentation.
How to configure your project to use it
- Verify your framework or tooling supports layered env files and loads .env.test.local after the base .env. If not, you may need to adjust your startup code or dotenv configuration.
- Create a file named .env.test.local in your project root and add test-specific variables (do not put secrets you wouldn’t share).
- Populate with values that help tests run deterministically, such as a lightweight database (often SQLite in memory) and stable API endpoints.
- Run your tests as you normally would. In many setups, the test runner automatically loads the environment and applies overrides from .env.test.local. If not, you can force the environment variable indicating test mode (for example, APPENV or NODEENV) before invoking the test command.
- Keep tests portable by excluding real secrets. Do not commit secrets into .env.test.local. Use CI secrets or a separate secret management workflow for production-like values.
- Consider adding a public .env.example or .env.sample to document expected keys without revealing values.
Sample workflow snippet
- Create .env.test.local with test overrides as shown above.
- Ensure tests initialize using APP_ENV or equivalent to pick up test settings.
- Run tests (e.g., npm test, phpunit, or pytest) as your project specifies.
Best practices for using .env.test.local
- Security first: Do not store real credentials in .env.test.local. Use dummy values or local mocks for development and testing.
- Ignore in version control: Add .env.* patterns to your .gitignore to prevent accidental commits of environment files.
- Provide a template: Include a .env.example or .env.sample describing required keys and their intended types.
- Separate concerns: Use distinct databases or in-memory stores for tests to avoid cross-contamination with development data.
- Align with CI: Mirror local test overrides in CI configurations where practical, but avoid embedding secrets in files checked into source control.
- Document expectations: Maintain a short guide in your repo describing how to use .env.test.local for new contributors.
Common pitfalls and troubleshooting
- Pitfall: Tests don’t reflect the overrides. If the framework doesn’t load .env.test.local, you won’t see test-specific values. Check the load order in your framework docs.
- Pitfall: Secrets leak. Never commit real credentials. Use placeholders and separate secret management in CI.
- Pitfall: Inconsistent environments. If local values differ wildly from CI, tests may pass locally but fail in CI. Keep test configs aligned and use mocks where possible.
- Troubleshooting tip: If a value seems ignored, print the effective environment in a test setup or log the loaded config to confirm which file was read and which values were applied.
Share This Article
Spread the word on social media
Anne Kanana
Comments
No comments yet. Be the first to share your thoughts!