amidknight
6 months ago

Laravel 5.8 Testing w/ Database Must Run One at a Time

Posted 6 months ago by amidknight

Odd behavior that could be coming from the not doing it correctly place. I'm also not sure what all information would help keep this succinct yet cover the issue - updates will be added ass necessary.

Consider a vanilla Laravel install.

// UserTest.php

class UserTest extends TestCase
{
    use RefreshDatabase;

    public function testOne()
    {
        $user = User::make(['email' => '[email protected]']);
        $user->save();
        $this->assertTrue($user->id == 1);
    }

    public function testTwo()
    {
        $user = User::make(['email' => '[email protected]']);
        $user->save();
        $this->assertTrue($user->id == 1);
    }
}

Running the tests one at a time, they both pass (phpunit --filter=testOne and phpunit --filter=testTwo). Run them sequentially, all but the first will error out with a timeout error (phpunit).

What's going on (I'd really like to understand the why)? And what's the fix?

What I've tried:

  1. Use in-memory SQLite.
  2. Use DatabaseTransactions instead of DatabaseRefresh

Would like to be as close to production-like as possible.

Please sign in or create an account to participate in this conversation.