According to the "Database Testing" documentation I can reset the database after each test (first option). Second option is to run test using Transactions. It seems a better approach to me, but if I want to run with transaction, the migration does not run.
Is there any way to run the migration once for all the test process?
In other words, I want to run the migration, run every tests with transaction, then rollback. I tried with what the documentation says, but I think something is missing.
Wrangled with this for a while today and running migrations in conjunction with migrations seems to do the trick. A snapshot of my test is as follows:
I've got a couple of factories in my actual test and they seem to run through the migrations with the data destroyed after the test as expected.
Here is a very portable and reusable way:
Then in each test file:
It is not possible to run DatabaseTransactions in combination with dusk for the moment.
https://github.com/laravel/dusk/issues/110
Database migrations work. So you should use those. Also make sure you run a seperate testing database so you don't mess with your production/development database.
https://laravel.com/docs/5.4/dusk#environment-handling
The provided answer works because DatabaseMigrations work. The
use DatabaseTransactions
is not relevant.From what I understand, I don't think transactions can ever work when using dusk, as each browser request in dusk creates a separate instance of your laravel app.
Previously, phpunit would create a new application in memory as part of the process (in the
setUp
/createApplication
method), then test against that testing application, then destroy it and set up the next one. As such, the transactions can be wrapped around (or just inside) the create and destroy parts of that application before it starts up a new database connection for the next test.With dusk, it's real end-to-end testing (including a browser, faked user interaction, the routing on your local machine, etc.), which means it is not all contained within the environment that your tests are running in, like they usually are in phpunit.
Dusk does the following:
.env.dusk.*
and launches the chromedriver (or whatever selenium-like thing you use)It's also worth noting that the
DatabaseTransactions
trait is in the Foundation package, not the Dusk package, so it's not build / packaged with Dusk in mind.This also explains why in-memory sqlite doesn't work with dusk, as one process does not have access to another process' memory.