kenprogrammer's avatar

Inflexibility when using Pest

In PHPUnit if I want to seed database before all the tests in a test file/class I'll put the model factories in the setup method like this which simple and straight forward:

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

        //Create login credentials
        $this->user=User::factory()->create([
            'username'=>'admin'
        ]);
															
       //Other model factories
    }

In Pest beforeAll doesn't work in a test file, you've to put it in Pest.php configuration file as a Global Hook or use beforeEach in the test file/class. Here are what I think are draw backs of either method:

  1. Using beforeAll as a Global Hook. If I've 10 tests files/classes and only three of them are using the hook it means the seeders will still run for the other seven tests even if they're not required by them.

  2. Using beforeEach in a test file/class. If I've 10 test methods in the test file/class and maybe there's one model factory that is required by only one test, it means the model factory will still seed the database for the other nine tests methods even if not required by them.

This results in longer tests execution time.

Is there something I'am not doing right when using Pest or that's how it's supposed to work?

1 like
1 reply

Please or to participate in this conversation.