How to specify a different .env file for phpunit in Laravel 5?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Separating Testing Configurations with Custom .env Files in Laravel 5 In the development of a web application using Laravel 5, one common pain point is handling test-specific configurations. While it's essential to maintain environment-specific variables in separate files (such as .env) per environment for security reasons, testing these variables can be tricky without causing conflicts. To make this process easier and safer, we can create a custom .env file specifically for PHPUnit tests. Firstly, let's examine the standard Laravel 5 .env file structure: ``` APP_NAME=Laravel APP_ENV=local APP_KEY=base64:gPw1KpXa0EI1/fQ9xjHtVG7hqv2sJm+z5OlDY55BH3E= APP_DEBUG=true APP_URL=http://localhost DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_test DB_USERNAME=root DB_PASSWORD=password ``` As you can see, this file contains the entire configuration data for your application's environment. However, we want a separate set of test-specific configurations for our PHPUnit tests. To achieve this, let us create a new .env file named _.env.testing: ``` APP_NAME=LaravelTests APP_ENV=local APP_DEBUG=true APP_URL=http://localhost DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_test_tests DB_USERNAME=root DB_PASSWORD=password ``` Here, we've modified the APP_NAME and DB settings to reflect our testing environment while keeping our original .env file in place for other environments. This separation will allow us to test specific configurations without affecting the actual application or any other environments. Next, let's configure PHPUnit to read from this new file during tests: 1. Open your phpunit.xml config file located at root of your Laravel application. 2. Add the following line to the configuration section: `` 3. Save and close the file. This code snippet tells PHPUnit to read a custom environment variable value for DB_HOST from your custom .env file (_.env.testing) when running tests in your Laravel 5 application. You can add similar configuration lines for any other environment variables you may need during testing. Additionally, consider using the phpunit.xml.dist as a base file to avoid overriding any existing settings. By following these steps, you've successfully separated your test-specific database configurations from your main application. This approach allows you to maintain a secure and stable environment for your Laravel application while testing without worrying about exposing sensitive data or causing conflicts with other environments. Remember that it's essential to maintain security practices in your development workflow, especially when dealing with environmental configuration files like .env. Be sure not to commit these files to your version control system and consider creating a dedicated repository for them if necessary. In conclusion, utilizing different environment-specific files with PHPUnit can make testing more efficient without compromising the safety of your Laravel application's configurations. For more information on this topic and other Laravel-related queries, visit https://laravelcompany.com/, where you can find comprehensive tutorials and guides to help enhance your development experience.