Skip to content

.env.testing not being loaded at all #55532

Closed
@VictorMLima98

Description

@VictorMLima98

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

  1. .env.testing is correctly placed in the project root.
  2. All caches were cleared: config:clear, cache:clear, view:clear, route:clear.
  3. app()->environmentFile() still returns .env.
  4. I've created an artisan command to debug it. Running php artisan my-command --env=testing loads .env.testing correctly, but running artisan test does not

Steps To Reproduce

  1. Create a .env.testing file in the project root with:
APP_ENV=testing
DB_DATABASE=testing
  1. Create a test like this
it('checks the environment', function () {
    dump([
        'env' => app()->environment(),
        'db' => config('database.connections.mysql.database'),
        '__env_file' => app()->environmentFile(),
    ]);
});
  1. 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions