What do you need to clean up after your tests?

Maybe less than you think

Joel Clermont
Joel Clermont
2024-05-22

I love getting replies from developers who read these tips. Last week, someone replied to my tip on avoiding logic in your tests with a suggestion to clean up my use of the Carbon::setTestNow() helper by calling it at the end of my test like Carbon::setTestNow(null).

It's a valid thing to consider. We don't want behavior inside one test to affect the state of the application in another test.

But thankfully we don't actually need to clean this up manually. Laravel's testing framework does it for us.

Inside the base test class, the tearDown() method already takes care of this:

if (class_exists(Carbon::class)) {
    Carbon::setTestNow();
}

If you take a peek in that method, you'll see a bunch of other things it does to reset the application to a clean state after each test. Consider this your homework, to go check out what else it cleans up automatically.

Here to help,

Joel

P.S. Is getting better at testing one of your goals for this year? Hit reply and let me know what you hope to accomplish.

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.