I'm trying to run Laravel Dusk and I need to use a test database. When I check the screenshot it says that the database doesn't exist. Problem is that is the database defined on .env and not the one on .env.dusk ... I've tried to rename the file to .env.dusk.local and still no luck. What am I doing wrong?
-
What was the answer? This has driven me crazy for a long time. When I run `php artisan dusk`, it ignores my `.env.dusk.local`, which I can prove by inserting `dd(env('APP_ENV'));` into DuskCommand.php. – Ryan Jul 07 '18 at 13:11
4 Answers
Try clearing your cache? I remember having similar issues when I first started using dusk.
php artisan cache:clear

- 98
- 1
- 6
-
Tried that, no luck. Tried to restart valet too and didn't work as well. – Bruno Teixeira Sep 11 '17 at 08:24
-
This did not help. I also have been under the impression that local and testing environments don't get cached by default anyway. – Ryan Jul 07 '18 at 13:12
I got the same problem. Lately I figured out that the .env file is being cached before dusk start.
I resolved it by adding
$this->artisan('config:cache');
into the createApplication() function in CreateApplication.php. It works :)
But then, if I finished the dusk test, I must run
php artisan config:cache
to get the original server running.
Edit:
later I tried simply call the
php artisan config:clear
also prevent the application caching the env variable. (Much simpler and faster)

- 61
- 1
- 3
I was able to get this to work by following these steps:
- Remove
.env.testing
- Run
php artisan dusk
(This will createphpunit.dusk.xml
). - Add the following to the end of this newly created file
<phpunit>
...
<php>
<server name="APP_ENV" value="local"/>
</php>
</phpunit>
- You can now restore
.env.testing
which can still be used for phpunit testing.
ORIGINAL ANSWER
I found that if you remove .env.testing
it then uses .env.dusk.local
. It seems that .env.testing
always take precedence.
This does not solve the problem of using a separate .env for unit tests & dusk; which is useful when running unit tests in memory and dusk tests using sqlite.

- 3,742
- 1
- 24
- 29
-
Not sure whether it's just my environment, but `phpunit.dusk.xml` is deleted immediately after `dusk` exits so it's not possible to save anything to it. It's entirely possible however that this is just another in a long line of bugs and generally abysmal issues that Laravel Dusk seems to ship with. – Hashim Aziz Apr 14 '22 at 22:37
Same issue... The problem is you forgot to create config cache file for your testing env so dusk will use your current local config cache file instead. Here is the fix:
1/ duplicate .env
to .env.dusk.testing
and edit the following:
APP_ENV=testing
DB_DATABASE=halfwish_test #I use different MYSQL database for testing.
2/ run this cmd:
$ php artisan config:cache --env=dusk.testing && php artisan clear //clear compiled services and packages files and cache config file.
$ php artisan migrate:fresh --env=dusk.testing && php artisan seed --env=dusk.testing //this is migrating and seeding demo data to DB.
Now you can run php artisan dusk
.

- 174
- 1
- 3
- 12