Skip to content

.env.testing not being loaded at all #55532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
VictorMLima98 opened this issue Apr 24, 2025 · 13 comments
Open

.env.testing not being loaded at all #55532

VictorMLima98 opened this issue Apr 24, 2025 · 13 comments

Comments

@VictorMLima98
Copy link

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.

@VictorMLima98
Copy link
Author

Just a note: I've just tested an app on Laravel 11 using the same environment described above and it worked fine.

@yoeriboven
Copy link
Contributor

Do you have APP_ENV set to testing in your phpunit.xml?

https://github.com/laravel/laravel/blob/f6e4638ee6ca1cd40aa7c56311d89ea3d91a24f8/phpunit.xml#L21

@lloricode
Copy link
Contributor

for work arround, just put this in your phpunit.xml

    <php>
        <env name="APP_ENV" value="testing"/>
    </php>

@yoeriboven
Copy link
Contributor

That is not a workaround. That’s where you set the environment so it knows which .env to use.

@utsavsomaiya
Copy link
Contributor

Image

@faissaloux
Copy link
Contributor

@utsavsomaiya which version of the framework you using?

@gsinti
Copy link

gsinti commented May 5, 2025

@VictorMLima98: I was having this issue just now and came here to look for others reporting it.

One thing that differed with my environment was that I had just upgraded from 12.11 to 12.12.

I ran composer install and it fixed the issue for me.

@VictorMLima98
Copy link
Author

@VictorMLima98: I was having this issue just now and came here to look for others reporting it.

One thing that differed with my environment was that I had just upgraded from 12.11 to 12.12.

I ran composer install and it fixed the issue for me.

Sorry for the late reply, guys. I've managed to get around this issue by downgrading my project a few minor versions. I'm using 12.8.1 and the issue no longer takes place.

I bet that's a problem that has been introduced on 12.10 or something like that.

I tried to debug the framework files for a few hours without success, so I just downgraded and boom, problem's gone.

@VictorMLima98
Copy link
Author

Actually it just happened again on 12.8.1.

Tried upgrading to 12.12 and it persists.

@gsinti
Copy link

gsinti commented May 5, 2025

@VictorMLima98: I assume after switching versions, you're running a composer install between each attempt to run tests?

@VictorMLima98
Copy link
Author

Sure! I even deleted my whole vendor folder and re-installed it, but no solution yet :/

@itpilotdk

This comment has been minimized.

@crynobone
Copy link
Member

Hey there, thanks for reporting this issue.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?

Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants