Should we write tests for this one-time command?

I'm a huge proponent of writing tests, but is there ever a valid time to skip them?

Joel Clermont
Joel Clermont

Let's keep the discussion of our one-time user import command going. In this tip, we'll talk about it from a different angle: testing.

Personally, I have found a huge amount of value in writing tests for my Laravel applications. Not only does it catch potential bugs, it provides guards against breaking this code accidentally in the future, and it generally leads to better code design.

So why did we decide to skip writing tests for this one-time command? There were a few considerations:

First, all the logic was being pulled in from existing known-good code in our UserController::store() method. So the risk of bugs is immediately lower.

Second, this wasn't a long-lived feature. We were going to literally run it one time, and then delete it in a future pull request.

Finally, just because we didn't write Laravel feature tests, doesn't mean the code was untested. We exercised it locally and in a staging environment, and then in production we ran it first with just a few records before importing the full batch of new users. And more than one person was involved in testing, not just the code author.

In 99% of use cases, I think writing automated tests is still the right answer, so take this tip in the context it was intended, not as an excuse to skip testing in general. But it's useful to recognize there are specific times when it can be a rational choice to skip them.

Here to help,


P.S. You think this tip is good? You should try our free book of even more Laravel tips to make your application a little bit better.

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.