I’ve heard time and time again, “Unit tests are a waste of time” or “Unit tests are too much work for too little benefit.” While I agree that poorly executed unit tests may not yield significant advantages, when done properly and with the correct intent, they can save you a tremendous amount of time in the long run.

In this post, I won’t be discussing how to write tests (I’ll cover that in a future post). Instead, I will explain why I personally write unit tests. I emphasize the “I” because this post isn’t about convincing you to write unit tests. The answer to every software-related question ever asked is “It depends,” and unit testing is no different. I cannot tell you why you should write unit tests because I don’t know your particular circumstances; they may be completely different. All I can do is share my reasons for writing them, hoping to provide solid anecdotal evidence of why unit tests are an essential software development tool.

To Fix Bugs

Unit testing is an incredibly powerful tool for fixing bugs. The very first thing I do when attempting to resolve a bug is to write a unit test that reproduces the issue. By doing this, I establish a quick and repeatable way to replicate the bug. I then apply the bug fix and rerun the test. If it passes, the bug is fixed!

Fixing bugs is also an excellent opportunity to introduce unit testing into a legacy codebase lacking tests. Creating unit tests to address specific problems is typically easier for those new to unit testing, and the benefits of the test become immediately apparent since they assist in fixing actual issues.

To Verify that the Code Does What I Expect

Whenever I write new code, I always ensure that it works before committing it. For me, unit testing is the quickest way to validate that the code does what I anticipate. The feedback loop of writing a test, running it, and making necessary adjustments is much faster than setting up the entire application, reaching the state where I can test the new feature, evaluating it, and then repeating the process.

This is not to say that I neglect full stack testing. I certainly strive to ensure that new features work in the full stack before committing them as well. However, I do this after running all my unit tests, which usually significantly reduces the time required for full stack testing. By the time I engage in full stack testing, the feature is usually 99% correct.

To Promote Clean Code

Unit tests are way easier to write if you follow SOLID design principles.

  • They result in fewer tests because the classes being tested are usually small and focused.
  • You can inject mocks that allow you to focus on testing the class being tested without setting up numerous dependencies.
  • Tests are more stable because they are smaller in scope and rely on fewer real code components to execute.

To Act as a Safety Net

By having a solid set of unit tests in place, I don’t have to do a full manual regression test of my application when I add new features, fix bugs, or refactor old code to make sure I didn’t break existing features. I get immediate feedback either from a compiler error in the tests or failed tests in a test run that something isn’t right.

Furthermore, unit tests act as a powerful time and cost-saving mechanism. With comprehensive test coverage, I can significantly reduce the amount of manual testing required. This translates into saved time and resources since I can focus my efforts on testing just the new features.

Moreover, the presence of robust unit tests helps to minimize the occurrence of production bugs. By catching issues early in the development cycle, I can address them promptly, reducing the chances of critical bugs slipping into production. This leads to improved stability and reliability of the software, resulting in cost savings by avoiding potential customer complaints, service disruptions, or the need for emergency fixes.

Conclusion

I write unit tests for several reasons: to fix bugs efficiently, to validate my code’s behavior, to promote clean code, and to provide a safety net that saves time, money, and reduces the chance of production bugs. While your circumstances may differ, I hope sharing my perspective on the importance of unit tests has provided you with valuable anecdotal evidence supporting their use as a crucial software development tool.

Remember, the decision to write unit tests ultimately depends on your specific situation, but when done properly, they can yield significant benefits and save you time and effort in the long run.