Learner
Learner

Reputation: 621

Laravel Dusk: Migrate and Seed Testing DB Once

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()
    { 
    }

}

Upvotes: 2

Views: 4041

Answers (2)

Artur Nartaiev
Artur Nartaiev

Reputation: 1

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

Upvotes: 0

Tushar
Tushar

Reputation: 1176

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.

Upvotes: 4

Related Questions