-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Closed
Labels
Description
Laravel Version
12.10.1
PHP Version
8.4.1
Database Driver & Version
MySQL 9.2.0
Description
.env.testing is not loaded when running php artisan test on Laravel 12
I'm running a Laravel 12 project, and I’m having trouble getting the .env.testing file to load when running tests via php artisan test.
According to the docs, .env.testing should be loaded automatically when running tests using Artisan. However, this does not seem to be happening in my setup.
Expected Behavior
Laravel should load .env.testing and use the environment variables defined in it.
Actual Behavior
Laravel loads the default .env file.
The environment remains local, and the DB_DATABASE is not coming from .env.testing.
This means tests are running against the development database.
Additional Info
.env.testingis correctly placed in the project root.- All caches were cleared: config:clear, cache:clear, view:clear, route:clear.
app()->environmentFile()still returns .env.- I've created an artisan command to debug it. Running
php artisan my-command --env=testingloads.env.testingcorrectly, but runningartisan testdoes not
Steps To Reproduce
- Create a
.env.testingfile in the project root with:
APP_ENV=testing
DB_DATABASE=testing- Create a test like this
it('checks the environment', function () {
dump([
'env' => app()->environment(),
'db' => config('database.connections.mysql.database'),
'__env_file' => app()->environmentFile(),
]);
});- Run the test
php artisan test --filter="checks the environment"You can also try to migrate the testing database with php artisan migrate --env=testing and see if your development database got refreshed, which is my case.
linktoahref and supun-crown