Quite often people ask the reasons of writing unit tests for their code. There are several benefits of writing unit tests that add value to the code as well as the developers writing the tests. I am listing a set of points here that came to my mind while thinking about the benefits of Unit testing.
Benefits to code:
- The code will have very few bugs as most of them are identified and resolved while running tests
- Silly mistakes are inevitable while writing code. Identifying them while running the application is difficult at times. Unit tests catch them instantly
- Code doesn’t need documentation. Names of the behaviours or methods defined in unit tests shout the purpose of their part
- Testable code is easily extensible too. Adding new features to the code becomes easy
- If Unit tests are written right from beginning of development, the underlying code automatically follows a set of quality measures that are otherwise very difficult to achieve
- After applying some modifications, the code can be shipped within very less amount of time, as most of the regression testing is also done by the unit tests
Benefits to Programmers:
- Unit testing teaches good programing practices to developers
- Unit tests force programmers to use some of the features of the language and framework that would otherwise remain just as theories in programmers’ minds
- As we write more code, our thinking about code starts maturing. With unit tests, we are bound to write a lot of additional code than the usual production code. So, it improves the programmer’s ability to think logically
- Programmers gain the experience of writing clean, loosely coupled code and learn the ways to avoid anti-patterns
- It becomes very easy for any new member to understand and start working on the code
These are just my opinions. The list is definitely not limited to the above set of points. Feel free to put a comment on anything that I didn’t mention in the above list.