What is the ultimate goal of unit testing? According to author, computer scientist and CTO Martin Reddy, “The purpose of unit testing is to isolate the smallest testable parts of an API and verify that they function properly in isolation.” This is accurate, but it doesn’t explain the actual benefits of unit testing that developers experience on a daily basis.
The Purpose of Unit Testing
When your codebase is large and complex, it can be hard to know exactly what will happen when code is modified. Essentially, the purpose of unit testing is to ensure that the code you’re working with is robust and resilient against accidental behavior changes caused by future code modifications.
Unit tests do this by telling you if the behavior changes in any of the units of code that you have unit tests for, which is why it’s important to have unit tests that cover as much of the codebase as possible: there is basically no other way to be sure your code’s behavior won’t be impacted (or broken) by modifications.
Having unit tests also saves a considerable amount of time that would need to be spent hunting for regression bugs and other issues, because they can pinpoint the exact units that have been impacted.
If you don’t have the time to write hundreds or thousands of unit tests yourself, there are tools that can help you get the tests you need by creating them automatically.
Goldman Sachs, for example, has used Diffblue Cover, a unit test generator that created thousands of tests in hours, rather than the weeks or months that would have been required for a developer to write them.