Skip to content
Latchkey

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
<!-- phpunit.xml missing the env override -->
$ php artisan test
  Connecting to DB_DATABASE=app (production default) instead of testing

Common 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

  1. Add env entries under <php> in phpunit.xml.
  2. Point the DB connection at the testing database or sqlite.
  3. Confirm no config cache is loaded during tests.
phpunit.xml
<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.

Terminal
php artisan migrate --env=testing --force

How 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.

Frequently asked questions

What causes "APP_ENV not "testing" in CI"?
Without <env name="APP_ENV" value="testing"/> PHPUnit inherits the default environment and uses the primary DB connection.
How do I fix APP_ENV not "testing" in CI?
Declare the testing env in phpunit.xml

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card