Search code examples
phplaravelunit-testinglaravel-5.4laravel-dusk

Migrate only once with Laravel dusk


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.


Solution

  • 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:

    <?php
    
    namespace Tests\Browser;
    
    use App\User;
    use Tests\DuskTestCase;
    
    use Illuminate\Foundation\Testing\DatabaseMigrations;
    use Illuminate\Foundation\Testing\DatabaseTransactions;
    
    class DefaultTest extends DuskTestCase
    {
        use DatabaseMigrations, DatabaseTransactions;
    
        /**
         * A Dusk test example.
         *
         * @return void
         */
        public function test_something()
        {
            //Add test stuff here
        }
    }
    

    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.