logo
podcast Podcast
get help Get Unstuck

Use global test setup methods sparingly

Seems like a good idea at first, but...

Joel Clermont
Joel Clermont
2024-07-23

As I work with teams on their Laravel apps, I find it is relatively common to see test setup methods used, both in individual test classes, and globally in a base test class.

On the other hand, in my own projects, I rarely use the setup methods. Why not?

Well, on the surface, a test setup method seems like a logical place to do common setup. Code reuse is a good thing, right?

Normally, I say yes, but personally I think tests are better when they're verbose, even if they're a bit repetitive. Seeing all the logic in one place makes it much easier to see what is going on.

Also, test setup methods might fit 90% of your tests, but I've never seen a situation where it fits 100% of the tests. So you end up needing to override the setup method in some cases anyway.

I'm not saying you should never use a test setup method. I'm only raising the discussion to suggest thinking about if they're really necessary.

Here to help,

Joel

P.S. Honestly, if you have a test suite, you're a hero in my book. So many projects I see don't have tests. We can help you get started, if that's you!

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.