What Does Unit Testing Have to Do with Agile Development?
I think the two are closely linked. In fact, I believe you can’t be truly agile without implementing automated unit testing as an integral part of the development process. Automated unit testing has several benefits that align closely with agile development principles.
The central benefit of unit testing is that it produces working code faster and with fewer bugs. The ability to automate tests and catch bugs at the development stage reduces a huge amount of overhead that is otherwise spent on releases that are immediately rejected by QA due to basic functionalities being broken. Unit testing increases the chances of a new feature working correctly upon first delivery, as it becomes the developer’s responsibility to verify that he is delivering working code.
Another reason that unit tests cut down on development time is that their fine resolution allows them to pinpoint pre- cisely the location of a problem. A failed unit test can direct the developer to the exact location of the problem in the code, allowing him to quickly resolve it. This minimizes or even eliminates the time that would otherwise be spent locating the problem.
Unit testing may not be able to catch all bugs, but it is highly effective in catching regression bugs that are defects that break existing functionality. These bugs hamper progress and waste valuable development and QA resources as code is sent back and forth between the two departments, delaying new versions of existing products and new product releases. Without automated testing, it is virtually impossible to detect bugs during the development phase. This causes sprints to become bogged down as developers need to spend more and more time fixing regression bugs in order to keep producing working software. It becomes impossible to maintain a steady and predictable software delivery schedule while also maintaining quality. When a release date draws near and the product is not working, panic sets in, software is released without enough time to test it, and more bugs are introduced, creating a vicious cycle.
Code that is not properly maintained very quickly becomes legacy code that developers either refuse to change or insist on rewriting themselves. To keep code alive, you need to be able to change it and be confident that your changes won’t break anything. Unit testing promotes this confidence. Without it, you end up either refusing to change older code or investing large amounts of time rewriting it every so often. In order to respond quickly to change, you need to be able to modify all parts of your code quickly and confidently. Some tools even allow you to develop unit tests for older code without having to change the code itself.
Agility through Automation
The platform for unit testing is implicit, and we usually omit the word automated before it. In reality, unit testing is a collection of processes, skills, and tools that support agility. For example, writing the tests is an actual skill. I look at tests I wrote five years ago and think, “How would anyone let me write this?” (I’m sure I’ll feel the same in five more years about what I’m writing now.)
In addition, using isolation and mock objects correctly is a capability that improves over time. Refactoring of the tested code or changing code design can fill up a three-day workshop, and much like design, it can be improved and lead to maintain- able test design.
When we improve our skills, we can move more quickly and change directions as we go with agility. But without automation, we won’t be able to use our skills effectively in a repeatable fashion.