As a developer, you may have many different reasons to try out the practice of test-driven development (TDD). But before you can even learn how it works and why it’s good, you have to answer the first question: “Why should I do this?”
Here are five answers that come up time and again.
1) You Need To Know What You’re Building
This is a fundamental reason for doing TDD: if you don’t know what your code should do when it’s finished, how can you tell when it’s right?
Tests give you a set of requirements that the build has to pass, and they’ll fail if there’s any problem. You should also write tests before you start coding so you know what to code and have an automated way of checking it later.
2) You Want To Keep Your Code Working
TDD is a programming practice, not just a testing one, which means that as well as writing the code, you write the unit tests at the same time. Once the test passes, that’s just the starting point for writing software. You need to continue maintaining it until someone tells you there’s a problem, and quizzes will help you do that.
3) You Want Your Code To Be Reusable
Suppose your test suite understands your dependency implementations and uses them only indirectly. In that case, you know that you can swap those implementations for others and still have the same tests run successfully. This gives you much freedom to change low-level implementation details because as long as they’re hidden behind a stable interface, your code will stay intact.
4) You Need Confidence In What You’ve Written
Tests are a form of documentation, allowing you to look back at what you were doing when you wrote the code. If it’s hard to understand why your test suite says the code is working, then it might be because there was a misunderstanding between yourself and your future self. Code that’s easy to understand promotes confidence in your work, which will benefit you now and in the future.
5) You Enjoy Getting It Right
TDD isn’t just about preventing mistakes – it’s also about making your code as good as you can. There are a lot of programming practices out there that involve different ways to write code and other ways to test it – find a style that feels good for you and your team. If you’re not enjoying the process of writing code and tests, then maybe that means you should be trying out something different.
6) There’s Always Room To Grow
If anything here inspires you to try out TDD, then go ahead! You can always learn more about new tools or techniques that will help you improve your code. But sometimes, some things might stop you from practicing TDD, and that’s fine too. If you want to try out TDD, but your codebase is huge or challenging to work with, then maybe it’s worth looking for a better approach before spending the time learning how to use tests.
TDD is a great practice to have, but it’s only worth the time if you find it helpful. If it feels like something hard to do because you’re not prepared, then maybe experiment with how you write tests before deciding on whether or not to use TDD. Remember that this kind of code craftsmanship can be challenging to get right, but your codebase will continue to grow and improve if you make an effort.