Why we like minimal seeders for tests

Getting this right will make your tests so much easier to maintain

Joel Clermont
Joel Clermont
2023-12-05

In yesterday's tip, we talked about how to use the $seed property for quicker test runs, but we glossed over what to actually put in the seeder.

Our approach is to have the default seeder be as minimal as possible. It should only contain data that is absolutely necessary for the application to run.

What do we mean by that? An application needs users, right? No, we don't put users in our seeders. We're talking about "system" data, like a list of countries or postal codes. Or you may have data specific to your application domain that just has to exist.

Think of it this way: The default seeder should only contain things that you would want to run in production when provisioning the application for the first time. So fake users or companies or transactions, none of that belongs in there.

The test itself should be responsible for setting up the database records it needs for that specific test. This keeps each test easy to read in isolation, and it prevents a lot of test flakiness that can arise from depending on data to be set up in a specific way outside the test.

Here to help,

Joel

P.S. Would you like help setting up a clean test environment in your project? It's a fantastic investment you'll benefit from for the life of your application.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼

Level up your Laravel skills!

Each 2-minute email has real-world advice you can use.