Laravel APP_ENV=testing not applied to tests in CI
Laravel picks config by APP_ENV. If tests run without APP_ENV=testing, they use the default .env connection instead of the testing one, which can wipe a real database or connect to the wrong service. Set the testing env in phpunit.xml or the job env.
What this error means
Tests connect to the development database, migrations run against the wrong schema, or RefreshDatabase truncates unexpected tables because APP_ENV was never set to testing.
<!-- phpunit.xml missing the env override -->
$ php artisan test
Connecting to DB_DATABASE=app (production default) instead of testingCommon causes
phpunit.xml does not set APP_ENV
Without <env name="APP_ENV" value="testing"/> PHPUnit inherits the default environment and uses the primary DB connection.
A cached config overrides the testing env
A config cache built for the default environment is loaded even during tests, so the testing overrides never take effect.
How to fix it
Declare the testing env in phpunit.xml
- Add env entries under
<php>in phpunit.xml. - Point the DB connection at the testing database or sqlite.
- Confirm no config cache is loaded during tests.
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>Run artisan with --env=testing
For migrate and seed steps, pass the environment explicitly so they use the testing configuration.
php artisan migrate --env=testing --forceHow to prevent it
- Set APP_ENV=testing in phpunit.xml, not just the shell.
- Point the testing DB connection at sqlite :memory: or a dedicated database.
- Clear any config cache before the test suite runs.