Undoubtedly, unit testing has a lot of benefits. Most of the time we hear about the huge advantages for customers (testing increases product quality and user satisfaction). Or about the benefits for maintenance (testing can decrease maintenance costs on the long run). But, I would like to focus on the advantages that testing bring to us, software developers. I feel that this topic does not receive the same level of attention as the others. I had the chance of using TDD in various .NET and Java projects, and I can tell you that it completely changed the way I develop software. Honestly, I cannot imagine working on a complex application without unit tests. And here is why…
1.Unit tests help you spot design flows and code smells
One of the things that you can improve with unit tests is the naming: the name of classes, the name of methods and the name of parameters. Are your names intuitive? Or are they completely difficult to grasp? This is something that will be obvious in your tests, because you will be forced to work with your API from the “consumer” perspective. Each time you write a test and you feel like it does not “sound good”, then it’s time for a little refactoring.
Unit tests can also help you with high coupling. If your test scenarios are very difficult to set up, and require a lot of dependencies, then this is a pretty clear indicator that your system is tightly coupled.
Last but not least, lack of abstraction can also be discovered and fixed with tests. If your system under tests depends on concrete components, that are difficult to mock, then you should probably check whether you can extract interfaces or abstractions out of them.
2.Unit tests help new developers get familiar with complex applications
Have you ever tried to explain a complex system to a new member of your team, only to see how confused they are after your explanations? Developers “speak code” and they understand code better than some drawings on a small piece of paper. Imagine you have a payment service that you would like a new developer to get accustomed with. Now imagine you have a strong suite of tests for this service. You can now ask your colleague to check out the unit tests and play around with that component until he understands how it works and how to use it. But the most important benefit is that he can put a break point and execute the test with the debugger without actually running the application in a real environment. You would be amazed by how much time you can save on familiarizing new programmers with your application if you have good unit tests.
3.Unit tests give you confidence
I worked on projects where programmers were too scared to change something because they had no idea of the consequences that their change would have on the system. You can imagine that over time, the code base just got uglier. This in turn made developers even more anxious to make important changes that could simplify the application. All of this can change if you have a strong suite of tests and a decent code coverage. Each time you make a change, you can run all the unit tests and be instantly alerted when something went wrong. It gives you confidence. The confidence to continuously improve the design of your application without fear of regression bugs.
I believe that software developers benefit most from testing. There is really no excuse for not writing unit tests. Each time you hear things like “Unit testing takes too much time”, or “It’s not our responsibility to write tests”, stop and think about how much time (and money) you will save by maintaining a poorly written application that is difficult to change and validate.