1

Is it possible to run migration and seeding once and don't refresh the testing db between the test methods?

I have couple of testing functions that depend on each other and I don't want to migrate and seed the database before and after each test in one testing file.

Example:

<?php

namespace Tests\Browser;

use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Carbon\Carbon;

class AdminTest  extends DuskTestCase
{
  use DatabaseMigrations;

  /**
   * Define hooks to migrate the database before and after each test.
   *
   * @return void
   */
   protected function setUp(): void
    {
        parent::setUp();
        $this->artisan('db:seed', ['--class' => 'DatabaseSeeder']);
    }

    public function testAdminCanLogin()
    {
    }

    /* Create New ticket */
    public function testAdminCreateTicket()
    { 
    }

    /* View the first ticket */
    public function testAdminViewTicket()
    {
    }

    /* Edit the first ticket */
    public function testAdminEditTicket()
    {
    }

    /* Assign the first Ticket to an Agent */
    public function testAdminAssignTicketToAgent()
    {
    }

    /* Unassign the first Ticket from Agent */
    public function testAdminUnassignAgentFromTicket()
    {
    }

    /* Delete the first ticket */
    public function testAdminDeleteTicket()
    {
    }

    /* Restore the first ticket */
    public function testAdminRestoreTicket()
    { 
    }

}
Learner
  • 611
  • 3
  • 12
  • 28

2 Answers2

4

Yes, You can do something like this

protected static $migrationRun = false;

public function setUp(): void{
    parent::setUp();

    if(!static::$migrationRun){
        $this->artisan('migrate:refresh');
        $this->artisan('db:seed');
        static::$migrationRun = true;
    }
}

Include this in your dusk test class. setUp method runs before each test method, If migration has run once, It won't run it again.

Tushar
  • 1,166
  • 4
  • 14
  • 31
  • I just saw your answer, it didn't work with me the second test didn't successed its redirected to login page insted on ticket page to create ticket, and when I copied the login code inside the second test (create ticket) it didn't successed as if the users table is rolled back! – Learner Feb 28 '20 at 19:19
  • @Learner you just need to remove the `use DatabaseMigrations` trait. So it will not reset the Database for each test. – Kidd Tang Dec 18 '21 at 09:10
0

don't use use DatabaseMigrations. just: $this->artisan('migrate:fresh'); $this->artisan('db:seed'); like:

public function setUp(): void
    {
        $this->appUrl = env('APP_URL');
        parent::setUp();
        $this->artisan('migrate:fresh');
        $this->artisan('db:seed');
    }

in your first browser test