Search code examples
phplaraveltestingphpunitphp-pest

Pest errors in random tests, what to do?


I have about 90 tests written with Pest for my Laravel application. Most of the time all the tests pass but sometimes even if I don't make any change to the codebase i get an error.

The error is the same but it occurs of different tests each time. The only thing the tests that fail have in common is the usage of the seed() function.

The database seeds correctly each time I use php artisan db:seed and php artisan migrate:fresh -- seed

  • Tests\Feature\Domain\Product\Resources\DeleteProductTest > it can remove a product
   InvalidArgumentException 

  You requested 1 items, but there are only 0 items available.

  at tests/Feature/Domain/Product/Resources/DeleteProductTest.php:17
     13▕     /** @var \Domain\User\Models\User $bob */
     14▕     $bob = User::factory()->create();
     15▕     actingAs($bob);
     16▕     
  ➜  17▕     seed();
     18▕ 
     19▕     $product = Product::all()->random();
     20▕ 

I am using the RefreshDatabase trait and I am clearing the cache before running the test batch.

Does anyone know why this happens or how to fix it?


Solution

  • Fixed it myself. There was some logic that relied on randomness and sometimes the database wasn't seeded properly.