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

llhilton's avatar

Migration for every test?

I've currently got 44 tests (mostly acceptance-test types), and each test is taking several minutes to run. If what I think I'm seeing is what's really going on, phpunit is migrating, seeding, and then deleting the db for each test. Is this what should be happening? My tests are painfully slow (several minutes each at least), but if it's running the migration, that would be why. (It's slow, and I haven't bothered to fix that yet.) Moving to sqlite improved things to the point that I don't ctrl-c out of the process in frustration, but not enough that I could keep doing this without wanting to throw my computer out the window. So, is it/should it be migrating for every single test? Is there a way to make it not do that but just do it for each run of phpunit? If I know it's just going to take a few minutes to start up, that bothers me a lot less than a few minutes for Every. Single. Test.

0 likes
4 replies
llhilton's avatar

ETA: In more digging, it looks like I'm calling the migrate and seed each time by using this in my TestCase.php file:

public function setUp()
    {
        parent::setUp();
        Artisan::call('migrate', [
            '--env'  => 'testing',
           "--database" => "sqlite"
        ]);
        Artisan::call('db:seed');
    }

    public function tearDown()
    {
        Artisan::call('migrate:reset');
        parent::tearDown();
    }

Rather than do that for each test, can I put these commands somewhere else so that I start phpunit, it runs the migration and seeding once, then does all the tests?

moehtet's avatar

I also want to know the answer of the topic. Who can answer it?

setnemo's avatar
<?php

declare(strict_types=1);

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Support\Facades\Artisan;
use Tests\Helpers\CreatesApplication;

/**
 * Class TestCase
 * @package Tests
 */
abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;

    protected static bool $setUpHasRunOnce = false;

    /**
     *
     */
    public function setUp(): void
    {
        parent::setUp();
        if (!static::$setUpHasRunOnce) {
            Artisan::call('migrate:fresh');
            static::$setUpHasRunOnce = true;
        }
    }
}

Please or to participate in this conversation.