By Rob Fisher, Lead Back End Developer in Mobas' Digital team.
Unit testing of code is something rarely done by developers or web design agencies, and it’s even rarer for those commissioning software to understand the huge benefits. Why developers avoid unit testing is a subject for another time: here we will look at the benefits for the project owners.
Our example
One of Mobas’ corporate clients has a requirement for a complex system of providing pricing based on vehicle rates, additional package rates and seasonal rates throughout the year. In addition, in some cases rates are overridden for weeks and weekends. The potential variations are huge and constantly changing.
User testing such a complex environment is near impossible and constant ongoing changes and enhancements would lead to regressions (new software bugs) which would endanger the system’s core operation.
The chosen solution was to use TDD (Test Driven Development), where a whole range of tests were written upfront, covering all aspects of system operation. Code was then written until all of the tests passed and, where necessary, the code was then refactored into more efficient units and tested once again.
Inspires confidence
As a part of our continuous integration process, unit tests are run hourly. If the tests pass, this essential aspect of the system is proven to be working perfectly. Conversely, should new changes have been made to the system causing regression, the tests would break allowing developers to take immediate remedial action.
As a result the owner of the system has solid evidence of a key and complex aspect of their system working perfectly, not just an overall impression or promise that the whole system probably works.
Better design from the outset
Developers who know that their code has to be testable write in a thoughtful manner facilitating testability. Code then tends to be written in smaller units, and lower complexity in code is both easier to understand and maintain. Issues are found early on as tests fail to pass and any problems have to be confronted early, leading to less bugs in the production system.
The owner of the system has a far more elegant and robust solution.
Easier and safer to modify and refactor existing functionality
Complex code bases get changed over time due to ongoing enhancements, patches and configuration changes. It’s a normal part of the program development process to test whether new units of code break old capabilities: this is called regression testing. Old tests are run against the new code to check whether they still work.
If, however, the original capabilities were built without tests, regression testing becomes almost impossible, As multiple code changes are made, bugs are far more likely to be introduced often in totally unexpected and hard-to-debug locations regardless of how carefully they are implemented.
Summary
As the owner of new software you are unlikely to have deep technical knowledge. A well unit tested codebase provides some peace of mind that the code base has some quality and is far more likely to cope with ongoing modification.