The Power of Unit Tests
Yesterday, my dev team embarked on a significant refactor of our main code base in order to merge in a branch alongside implementing new functionality and changing some existing classes. A trifecta of complexity if you will. For a production quality system, one that we expect to deploy very soon and is the backbone of our company’s infrastructure, you might expect us to have been a bit more careful / timid in making our changes.
However, you’d be wrong and it’s all because of unit tests (that and we are all rock-star engineers, but that’s another post).
I didn’t work with unit tests much before my current position. This was mostly due to legacy platform limitations that did not make automated testing easy. Now, I couldn’t live without them. Confidence in my work is much greater now than it was previously for a good reason: unit tests give engineers power.
I know that the requirements of a particular class are laid out in the classes’ test. Whatever I do to the class, it must not only compile but light up green. This gives me the boldness to make large changes without worrying that the refactor is going to negatively impact (read: break) other parts of the system.
Code these days often exists beyond the classes themselves and in places like configuration files and build scripts, and we’ve created tests that take these pieces of auxiliary setup into account. Doing that extends our confidence beyond our primary classes and into our build process, database connections, and system as a cohesive whole. Very cool.
Unit tests are great for creating interfaces (working with the API before you actually write the implementation is a good way to make sure it doesn’t suck), verifying complex functionality, and system integration. The most empowering thing they offer, however, is confidence. In an agile shop such as ours, this gives us the ability to work as quickly and fluidly as possible and is a huge factor in why our little team has been so successful.
“A compiler tells you your code is well formed, a unit test tells you your code is well behaved.”
– No Fluff Just Stuff, Fall 2008