Why to unit test your code
September 07, 2019
Software Testing is an important step in the process of software development. Having a well tested system does not only reduce the chance of having errors in the application, but can also lead to better software design and more maintainable code. In this article we want to have a look at the Why of unit testing.
Why do we test
Amongst many other benefits unit testing promises an increase in code quality, easier changes and higher agility, better documentation and even a decrease in cost of software development. Let’s have a look at each of those promises in detail.
Better code quality
There are several factors in unit testing that help us with the quality of our code which already begin before we even write the first line of code for our tests.
The first and one of the biggest advantages in testing is that it forces us to think about how the code under test should act in different cases. It is easy for us to fall into the trap of just thinking about the happy-path while we write our code and to never give weird edge cases a thought. When we need to test our code this is different. In addition to verifying that our code handles the case from the happy path we also think about how our code should respond to edge cases and invalid inputs. Considering these factors leads to code that is less likely to break in uncommon situations and even primes our mind to consider edge cases while writing code that we do not currently test. However, more resilience is not the only advantage that testing provides for the code quality.
The design of our code also benefits indirectly from writing tests. When we want to test a certain piece of code we must usually be able to provide some inputs, invoke part of the functionality and then verify the given output. This can become very difficult if our classes and functions are not properly structured and a certain class or function is too long or does too many different things. Testing our code therefore leads to a better design because we will end up with functions that only perform a single tasks and not multiple ones.
Easier changes and higher agility
Luckily we are moving away from trying to specify all our requirements upfront and to only start afterwards to build a system that will never change. Instead, we want to be agile and to change and enhance the capabilities of our system according to the current needs of our clients and customers.
But changing software always bears a risk of parts of the code breaking. It is not practicable to manually recheck every case in a large software system on each change for possible side effects. So how can we be sure that our system still performs correctly when we frequently apply changes? The answer in this case is unit testing. With tests that automatically verify that the code still behaves in the way it needs to in order to support existing behaviour we can easily verify that our changes did not break any existing behavior.
In practice certain parts of the code can behave in ways that are not immediately obvious to the reader of the code. A functional call might need to change a specific part of the state or to produce slightly different results in only some of the cases. The need of the code to act in this way might not be clear to somebody having a glance at the method or the outputs that are usually performed by the method. In cases where we are not able to refactor our code to make the given behaviour clearer we can use tests as a means of verifying it and to provide documentation. If a piece of the code gets refactored in years and a test fails that asserts and describes a certain behaviour the developer seeing and trying to understand the test will have a piece of documentation that he or she can’t miss.
Unit testing has several benefits that are not immediately obvious. Some times it might seem to take too much effort, but usually this is not a problem of unit testing itself, but a problem of how the code is written. By writing tests we increase the quality of our code and make it easier for us and others to change and adapt the system in the future.
A blog by Manuel about everything related to software development