Go back to blog listing

Establishing a Change-Detection Safety Net with Unit Testing

safety netWorking on a code base that has minimal tests or no tests is like walking on eggshells: every move you make has the potential to break something. However, with software, the damage is not always immediately obvious.

Before you touch another line of code, start building a unit test suite that serves as a change-detection safety net. This way, you can rest assured that you’ll be alerted if modifications impact application behavior.

If you know what the use cases for the existing functionality are, you can start by “tracing” unit test cases as you execute the use cases. This way, you’ll get a set of test cases that you can run daily to ensure that you’re not breaking or changing this core functionality.

Another option is to automatically generate what we call a behavioral regression test suite: a baseline unit test suite that captures the code's current functionality. As we've  said before, test suite maintainability really diminishes above a certain code coverage level, so if your tool gives you a choice between generating a maintainable test suite and a high coverage one, choose the maintainable one unless you are trying to satisfy a coverage mandate.

To detect changes from this baseline, ensure that the evolving code base is automatically run against this test suite on a regular basis—ideally, daily. You’ll be alerted when the baseline functionality is impacted, and as you explore these impacts as part of your daily unit testing process, you’ll eventually learn more and more about the existing functionality.

As you start, remember that because today’s applications are so complex, your quality efforts and regression test suites also need to be complex. Unit testing is a great starting point… but it’s not a religion. Unit testing alone cannot deliver quality, will not expose all of your defects and change impacts, and is not a silver bullet.

If you really want to build quality into the code and gain a 360 degree view of how your daily modifications affect an existing application’s functionality, you need a continuous quality process that includes everything from static analysis, to peer code reviews, to unit testing, to functional & load testing. It takes some effort to get all the components established and working together, but the payoff in terms of team productivity as well as improved application quality is tremendous.


Photo credit: tanakawho

Stay up to date