As we work through the effects of COVID-19, F1 Solutions would like to assure you that our current professional and hosting services are and will continue to operate. As we are assisting many of our clients with rapidly establishing remote working arrangements, we are experiencing higher than usual call volumes on our support phone lines. We would like to thank you for your patience and understanding during this challenging and critical time.
30 Mar 2016

There is much debate about unit testing and its effectiveness in software development. Some software developers love it. Others claim unit testing is a waste of time. So it begs the question: is unit testing worth the effort?

What is unit testing?

Let’s start by looking at what unit testing is. An effective software development unit test is one that places an individual piece of code, such as an algorithm, method or system, into a certain state. Then you perform an action on it and ensure the resulting state is as you would expect. Unit tests should be self-contained and isolate the component from others, where possible. Unit tests should also be fast to perform. 

What’s the problem with unit testing?

The problem is writing repeatable and automatable unit tests is often seen as an activity that adds little value to the development process. At least that is the case in the early days. For this reason, it is often difficult to have time allocated to develop tests. This is twice as true for existing code.

Hands down, the biggest difficulty in introducing unit tests to existing code is that much code was not originally written in a way that lends itself to easy testing.  Codebases that were written 10 years ago are unlikely to use testing enablers such as dependency injection.

7 Benefits of unit tests and test-driven development (TDD)

1. Confidence - Automated unit tests give you confidence that the system will work as you think it should before having the quality assurance team test the system, i.e. issues and defects can be identified early and fix before they impact other teams. Once unit tests reach a certain saturation point, the team can have some level of confidence that the software will mostly work because all tests are passing. A win for management as it is more cost effective and a win for developers as there are less defects to resolve!

2. Impact - Developing automated unit tests not only ensures that a bit of functionality works as expected, it also means that if future changes affect the functionality of that part of the system, it will be clear and apparent earlier on in the process.

3. Simplicity - Writing code in a way that it can be tested by automated processes results in quite different code to code that is not written to be testable or with testing in mind. Code that is written to be testable tends to be cleaner, simpler and better separated which is a big win.

4. Maintenance - By writing code that can be tested using Unit Tests usually brings significant readability and comprehension improvements.

5. Refactoring - By building tests before modifying existing code it allows us to ensure that functionality is not broken unintentionally when making changes or refactoring code.

6. Completeness - TDD helps you to realise when to stop coding. Your tests give you confidence that you've done enough for now and can stop tweaking and move on to the next thing.

7. Design - Unit Tests help you really understand the design of the code you are working on. Instead of writing code to do something, you are starting by outlining all the conditions you are subjecting the code to and what outputs you'd expect from that.

So... Is unit testing worth the effort?  Absolutely! We think so.

When code is written alongside automated unit tests or written to be easily testable. It tends to be simpler, more readable and higher quality.

This has huge benefits when you consider code is read 10 times more often than it is written.

Simple, high quality code may take more time up front, but it saves you a lot in the long run.