logo
podcast Podcast
get help Get Unstuck

What do I put in my base test class setup method?

They aren't completely empty

Joel Clermont
Joel Clermont
2024-07-30

Last week, I explained how I use global test setup methods sparingly. This sparked a comment from a member of our Mastering Laravel community about two things he frequently puts in that base test class:

  • $this->withoutVite()
  • Http::preventStrayRequests()

This is an excellent point of clarification, and I use those in my base test class as well.

In addition, for the base test class for our Unit tests, we also bring in our own helper $this->alertUnwantedDBAccess(), which throws an exception if we query the database in any unit test. It keeps us honest with our intention to keep unit tests isolated.

So why are these types of methods okay in a base test class, but not the other types of setup logic I mentioned in the last article?

To me, these are broader environment setup methods, and not data that will be used by the tests. They define testing behavior, not testing logic.

They also are appropriate for all tests in the suite, which as I commented in the other article is very rare for data creation done in a setup method.

Here to help,

Joel

P.S. Want to see what our community is like? Did you know all of our books have an option to join the Mastering Laravel community for a four-week trial period?

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.