The importance of Unit Testing, or how bugs found in time will save you money
Unit testing still causes controversy among developers and product managers. There are both opponents and supporters of this kind of testing. In this article, I’ll highlight the main advantages of unit testing and explain why it is so important.
What are unit tests? Why bother writing them? How can they help developers and business owners? For the answers to these questions and more, read on.
Let’s start with the definition: Unit testing is a software testing method where “units”—the individual components of software—are tested. Developers write unit tests for their code to make sure that the code works correctly. This helps to detect and protect against bugs in the future.
Sometimes developers write unit tests first, then write the code. This approach is also known as test-driven development (TDD). In TDD, requirements are turned into specific test cases, then the software is improved to pass the new tests. In this approach, no code is added that hasn’t been proven to meet defined requirements. Unit testing is similar, in that it allows developers to modify code without affecting the functionality of other units or the product, as a whole.
Unit tests are usually written in the form of functions and check the value and behavior of these functions in various scenarios. For example, let’s imagine a function for the division of two numbers: the developer decides to follow the TDD approach, first writing a test with the input of values ‘4’ and ‘2’ (4 divided by 2) with ‘2’ expected as the result. Another example: when the divisor is zero, we don’t expect that the function will produce a value—we expect that it will generate an exception. We can expect that the function will notify some component about an attempt to divide by zero. Thus, we test two cases:
- In an invalid situation, the function will notify us that we are doing something wrong (an exceptional situation)
- The function will identify this invalid situation and log it
Four Advantages of Unit Testing
Some developers underestimate the importance of writing unit tests. What follows are five benefits of unit testing that you may want to consider before forming your own opinion.
Any Bugs Are Found Easily and Quicker
Code covered with tests is more reliable than the code without. If a future change breaks something in the code, developers will be able to identify the root of the problem right away rather than combing through an unwieldy codebase to find the issue.
Best practices suggest that developers first run all unit tests or a group of tests locally to make sure any coding changes don’t disrupt the existing code. However, consider the human factor: A developer might forget to run unit tests after making changes and submit potentially non-working code to a common branch. To avoid this, many companies apply a continuous development approach. Tools for continuous integration are used for this, allowing developers to run unit tests automatically. Thus, any unwanted changes in the code will be detected by a cold, logical machine.
The speed of detecting non-working code depends on the tools used for continuous integration. Tests can be set to run either a one-time check at a certain time interval or can be run immediately in real-time to review changes.
In short, unit tests help developers detect problems immediately, then fix them quickly. With fewer resources spent finding bugs, teams can move on to the next phase of a project.
Unit Testing Saves Time and Money
In his book, “Code Complete,” Steve McConnell shares a table with bugs and the cost of fixing them at different stages of the product lifecycle. The table shows that the earlier a defect is detected, the lower the cost of its correction.
When unit tests are written, many bugs are found at the software construction stage, which prevents the transition of these bugs to the following stages, including after the release of the product. This saves the costs of fixing the bugs later in the development lifecycle and also brings benefits to end-users, who don’t have to deal with a buggy product.
Unit Testing is an Integral Part of Extreme Programming
Unit tests are a prerequisite for the Extreme Programming methodology. Extreme programming is essentially a “test-everything-that-can-possibly-break” strategy. Writing unit tests with this methodology makes development and code refactoring simpler, integration easier, and creates living documentation. Which brings us to our next point…
Unit Testing Provides Documentation
Unit tests are a kind of living documentation of the product. To learn what functionality is provided by one module or another, developers can refer to unit tests to get a basic picture of the logic of the module and the system as a whole. Unit test cases represent indicators that carry information about appropriate or inappropriate use of a software component. Thus, these cases provide the perfect documentation for these indicators.
R2: Reusable and Reliable
Within unit tests environments, the individual modules of a product become isolated from one another and have their own area of responsibility. That means that the code is more reliable—it’s been tested in a contained environment—and therefore, reusable. Reusable code is a win-win for all: It’s clean, efficient, and consistent. All of this is accelerated with unit testing.
Covering Code with Tests
Okay, so unit testing is great, but how much test coverage is necessary? American software engineer Robert C. Martin, also known as Uncle Bob, argues that the goal of test coverage should be that 100 percent of the code is covered. Opinions among developers on this issue differ: some support a complete code coverage policy, while others consider this practice redundant—a topic too lengthily for the purposes of this article. In any case, when writing unit tests, you can use from many tools that determine the total percentage of the coverage of a project, separate module, or function. These tools are also able to graphically display the code sections covered with tests and indicate which sections in the code make the most sense to write unit tests.
Unit testing may seem like a tedious process, at first, but in the long run, it’s benefits are clear. Unit testing ensured that all code meets quality standards before it’s deployed. This ensures a reliable engineering environment where quality is paramount. Over the course of the product development lifecycle, unit testing saves time, money, and helps developers write better code, more efficiently.