Unit Testing and development: Build tests before you code - Bluefruit Software

Your browser is no longer supported. Please upgrade your browser to improve your experience.

Written by Jack Skerrett (Software Test Engineer) and Lewis Matthews (Junior Software Engineer)

Why unit testing is (probably) more important than your code

Imagine you’re a software tester. You have a way to see that code wasn’t behaving as expected, but before you even got your hands on it.
Imagine you’re a developer. You have a flag telling you exactly where your code wasn’t working.
Imagine you’re a product owner. You have this rapid turnaround between finding and fixing problems, saving your project time and money.
All this is achievable through unit testing.
Unit tests are a means of code quality assurance that we pride ourselves on using, here at Bluefruit.

What is unit testing?

Like the name suggests, unit testing is a process of testing software. But it has many more benefits than confirming that code “works”.
Unit testing focuses on very small sections of code which perform individual tasks, rather than the behaviour of the software as a whole. The sections of code can be as short as one or two lines. It is this quantisation of a project which lends so many advantages to a development team.
If later work, or even refactoring of the code, has a negative effect on the operation of the code then developers are notified of this immediately by failing unit tests. This testing process helps prevent bugs appearing in older areas of the software.

Unit testing puts efficiency and verification first

Using unit tests helps teams at Bluefruit to code within an Agile methodology. It provides engineers with confidence and structure, influencing their coding style and assisting them in creating effective and efficient code.
It also ensures that through the whole development process, teams know that all work going into the project is continuously verified and working in line with expectations. Recorded metrics show how much code coverage there is on a given project, providing warnings if this gets too low.
Developers are as likely to write unit tests as software testers, dependent upon how an individual team works. At Bluefruit, many of our teams’ developers are responsible for unit test and this enables the development of test code in symbiosis with the functional code. It often makes sense to write the test first and then write as much code as needed to allow the test to pass. Doing this moves towards a practice known as Test Driven Development (TDD). Bluefruit uses a lot of TDD because it helps us to build the right product without waste and redundancies.

A test first approach works

Writing tests first influences the entire architecture of the finished product. This approach…
… means that for each small step of development, the unit testing process forces engineers to consider the exact requirements of the specification before beginning to produce the solution.
… ensures that all code in a project is required, making the project cleaner and more efficient.
… guarantees code is cohesive and well structured, as the thought process of designing tests means that the current piece of work builds directly upon the existing code base. It also ensures code is not tightly coupled as functional blocks are kept distinct to enable focussed testing.
Unit tests provide further advantages to the project. They create an accessible reference manual for the project and often remove the need for lengthy technical documentation. How? The tests provide a specification for how the software should perform.
At Bluefruit we stay away from lengthy specification documents that dictate how a system should operate. Instead, we explore options that allow more flexibility and adaptability such as unit test writing.

Current and future benefits

Project costs are further decreased by unit testing as it reduces the time needed for testing and debugging software. Engineers are led directly to the source of a problem when bugs occur and can resolve them more quickly and efficiently.
In the later life cycle of software, unit tests continue to be of value and benefit the product owner. Having a pre-existing set of tests allows a development team to come back to a project, months or years, after initial development. The team can then make changes and updates with speed and confidence. This reduces the time needed for software testers because they will have a higher level of confidence when changes are made to older areas of the code.
But remember: having passing unit tests should never inspire 100% confidence in a tester. They are only testing individual units of code and can provide no insight into how the developed system will provide as a whole in the wild.

Test first, code second

Taking a unit testing first approach means that you start with a strong foundation on which to build and a means to validate it. All code used is relevant and has a distinct purpose, ensuring that everything is relevant to the project. Plus it helps you make sure that the work you do doesn’t undo the work you did before.
And as one of our developer says:

“If I was given the choice of losing all of my code or all of my  tests, I would choose losing all of my code.”

Your next project

Contact Bluefruit today to find out how we can help you on your next embedded software project.