Laravel 5.8 .env.testing file is not working Laravel 5.8 .env.testing file is not working laravel laravel

Laravel 5.8 .env.testing file is not working


I was having this same problem with Laravel v5.6. I set up a .env.testing file with a different username, password, and database but my tests were always running in the main .env database.

It looks like you need to specify the test environment when running the config cache command. After running this command, it worked as described in the docs:

php artisan config:cache --env=testing


This is what worked for me:

In phpunit.xml I had to define <env name="APP_ENV" value="testing"/> inside the <php> block to make PhpUnit load .env.testing instead of .env


It seems like the Laravel docs are wrong and .env.testing is not hard-coded anywhere in Laravel or PhpUnit, it reads the environment file for whatever APP_ENV is specified in phpunit.xml. You could even rename this to .env.phpunit or anything else if you define it in phpunit.xml in this format: <env name="APP_ENV" value="phpunit"/>


I was having the same issue with Laravel v7. Had my hands in my hair for quite some time, but I found a workaround.

The .env.testing was a lost cause for me. I never got it to load, so I just put all the variables within phpunit.xml. To make the testing environment (php artisan test) actually load those variables, I had to do this:

First I had to set APP_ENV in phpunit.xml to force="true":

<php>    <server name="APP_ENV" value="testing" force="true"/>    <server name="BCRYPT_ROUNDS" value="4"/>    <server name="CACHE_DRIVER" value="array"/>    <server name="DB_CONNECTION" value="sqlite"/>    <server name="DB_DATABASE" value=":memory:"/>    <server name="MAIL_MAILER" value="array"/>    <server name="QUEUE_CONNECTION" value="sync"/>    <server name="SESSION_DRIVER" value="array"/>    <server name="TELESCOPE_ENABLED" value="false"/></php>

Then I had to run php artisan config:clear, NOT :cache. When I ran php artisan config:cache it actually would not work. Probably because it caches the env once and never loads the new settings based on the different environment.