Unit tests are my passion. I always write them and I also encourage others to write them and even have taught developers to write unit tests. But I am quite surprised how often developers don’t write unit tests. There is a lot of benefits for writing unit tests. In this blog post, I will explain three main reasons why every developer should always write unit tests.
Know Your Code Really Works
When you have unit tests, you really know that your code works. You don’t have to say “I suppose it works”, “it should work”, “it worked yesterday” because you can say “it really works and I can prove it by running unit tests”. No conditionals, no ifs, no excuses, because you know that it works.
If you don’t have any unit tests how can you be sure that your code works? The only way is to test it manually. And that isn’t funny at all and takes a lot of time. Will you even remember to test all situations? Will you bother to test error handling? I guess most of the developers who don’t write unit tests just skip most of the testing. I have to admit that I did so many times when I didn’t yet write unit tests. I tested but test coverage wasn’t good most of the times.
How can you consider yourself to be a professional if you do not know that all your code works? How can you know all your code works if you don’t test it every time you make a change? (Robert C. Martin, “Uncle Bob”, in The Clean Coder)
Write unit tests and you can be really sure that your code works even after your co-worker has made a change to it.
Unit Tests Save Time, a Lot of Time
One of the main excuses to not write, or allow to write unit tests, is a misunderstanding that writing unit tests take time. Developers and managers think it takes double the time to code because you will write both actual code and unit tests. And you also have to maintain unit tests besides actual code. But if you look just few steps forward, you will realize that writing unit tests will save time.
How will you test your code if you don’t have unit tests? Yes, manually. Is it faster to test manually? Maybe for the first time it is faster to test manually than write a unit test. But when you have to test same code for the second time, unit tests have already saved you time. And after the third time, they have saved you a lot of time.
Imagine that after few weeks there is a need to make a change to your code. If you didn’t use the time to write unit tests earlier, now you have to test your code manually again. Or even if it was your co-worker who has to make the change and test it manually; he will have to do the same manual testing again that you have done earlier. “Saving” time by not writing unit tests at the beginning resulted using much more time for testing manually same things again and again.
If you had used just little more time at the very beginning, you would have saved a lot of time from manual testing because you could’ve run unit tests automatically and saved time. But remember, it is never too late to begin to write unit tests. Begin now and start to save time.
You Will Write Better Quality Code
Unit tests will help you to write better quality code because you can refactor code more easily. Maybe you have faced the situation that you find (or write by yourself!) ugly code but you don’t dare to refactor it for fear of breaking it. But if there were unit tests you dare to refactor it and make it more readable and maintainable. Refactoring is a key point for better code and with unit tests, it is even easy to do! All of us have made 100% sure refactoring which broke the code. Unit tests will make these refactorings really 100% sure.
Unit tests, especially if you write tests first (TDD), will help you to write better code. Usually code with unit tests is better quality than code without unit tests. At least it is much easier to write good code with unit tests than without them. The reason is that it is difficult to write unit tests for bad code, it is much easier for good code. For example, if you have a long method that does this and that (violating the single responsibility principle), it is difficult to write a unit test for it, compared to a method that does only one thing. Or compare methods that take seven or one parameters. It is easier to write tests for the method with just one parameter and methods with fewer parameters are usually better. Unit tests will guide you toward better code.
In my opinion, refactoring is one of the key benefits of the unit tests that is underestimated. With unit tests, you can refactor code more easily and much safer. By making refactoring easier it will improve code quality a lot and you are able to write clean code. And as we know, the good quality code is easier to maintain and to create new features and thus saves time and money.
Conclusion
I mentioned following main benefits of writing unit tests:
- You know that your code really works,
- It saves a lot of time and
- You will write better quality code.
There is also more than those three. Just few to mention: less regression, easier to test error handling, documents the code usage, etc.
There is a myth that writing unit tests take too much time. In my opinion, they will definitely save a lot of time, which is a really good reason to write unit tests. At the very beginning writing unit tests will take some time, but learning takes always some time. Start learning to write unit tests, don’t be too busy to improve.
One thought on “Always Write Unit Tests”