“How do you think when writing tests?” — It’s simpler than you may think
In this episode, we reply to an important question raised by a member of our community.
“I’m trying to understand how you think when writing tests. Do you just try to cover everything that comes to mind or you have some kind of list with priorities? Or you just try to reach max possible coverage? ”
We follow the basics. Our main process is to write the test first, see the test fail (red), then write the minimum amount of code to make the test pass (green), then refactor the code/test *if needed. By following this process, we end up with pretty high coverage (not always 100% but very close to it). The coverage is not a direct goal. The coverage is more like a positive side effect of following the process.
With that said, it requires discipline and skill to *always* write the test first. You may get stuck in the beginning. We’ve been doing this for many years, and we still get stuck sometimes! However, the goal is never to be stuck for long. It’s wasteful. At the end of the day, shipping is more important than testing first.
When you’re stuck, spike some ideas. Go free, without tests (or anything else) in your way. Play with the code (Xcode Playgrounds, Swift REPL, Debugging…) until you’re unstuck and found the solution you are happy with. Then think, “now that I know what I want to build, how could I have written the test first?” When you have the answer, you can discard the spiked solution (commit the spike solution into a separate spike branch), and start fresh, by writing the test first. Next time you have to solve a similar problem, you know how to do it test-first without spiking.
We practice, learn, and get a little bit better every day. The daily practice compounds!
Subscribe now to our Youtube channel and catch free new episodes every week.
Originally published at www.essentialdeveloper.com.
We’ve been helping dedicated developers to get from low paying jobs to high tier roles — sometimes in a matter of weeks! To do so, we continuously run and share free market researches on how to improve your skills with Empathy, Integrity, and Economics in mind. If you want to step up in your career, access now our latest research for free.
If you enjoyed this article, visit us at https://essentialdeveloper.com and get more in-depth tailored content like this.