Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jan_zikmund's avatar

Dusk tests without breaking live database?

Hi guys, I just started writing my first tests in Laravel 5.6 but I am confused about the best setup for the tests not to mess live database? I would like the tests to be available on both my local and on production so I can test with real application after I deploy a change, can this be done?

I use both Dusk and PHP Unit, and Dusk doesn't seem to support DatabaseTransactions, which would otherwise be my preferred option. If I use RefreshDatabase or DatabaseMigrations, they will afaik reset the DB, and if I don't use them, then test data will appear in live tables, so my latest assumption is, that Dusk tests cannot be run on live site, is that so or am I missing something?

I've read something about having a separate database for Dusk tests, also I could possibly mock some of the functionality but I would prefer a general solution where tests are run on whatever data are there at the moment, but the site would return to the state before test after testing is finished.

Or is there a better/recommended setup that I am missing? Thanks a lot.

0 likes
2 replies
Dunsti's avatar

From the Laravel-Documentation:

In addition, you may create a .env.testing file in the root of your project. This file will override the .env file's variables when running PHPUnit tests or executing Artisan commands with the  --env=testing switch.

Just set up another database-connection in your .env.testing file and run the migrations, so you have your database-structure.

I haven't done much testing yet, but from what I saw so far, many use Sqlite for this.

jan_zikmund's avatar

Hey Dunsti, thanks for pointing me in the right direction. Dusk seems to have a similar feature using .env.dusk. config file, so I just pointed Dusk tests there and use DatabaseMigrations in each test so database is only populated during the test and then emptied again. Will do just fine :)

Please or to participate in this conversation.