What is Unit Testing?

(This is an except from an article written by Christian Cantrell titled "CFUnit Testing Components", included on Macromedia's DRK 3)

Unit testing refers to the practice of developers writing code to test code. It is most important in circumstances where developers write "low-level" code or software components that are intended to be used by other components or otherwise extended.

There are two primary ways that unit testing differs from end-user or QA testing:

  • The code it tests is generally not accessible from a front end or user interface. It's often lower-level than presentation code.
  • The code it tests is easily quantifiable and measurable. This means it can be tested programmatically; a subjective tester doesn't have to test it.

Writing unit testing code for software components is a good idea because you can catch bugs in low-level code before they reach the user-interface level, where they are often quite difficult to track down.

The importance of unit testing code increases the more you are able to reuse and layer your code. Unit testing is also invaluable when you decide to refactor software components. Unit tests can tell you instantly whether your refactoring actually broke the component or other components that depend on it.

Unit testing is extremely important in environments where multiple developers work on a single project. For instance, if I am writing an application that calculates loan amounts based on real estate information in a database and one morning I update my source tree with all the latest code, it's going to be extremely inconvenient and a huge waste of my time if I update a bunch of little bugs checked in by other developers. If the other developers unit tested their code before checking it in, however, they could catch most of their bugs before they would affect other people's work.

In the past, there was not much need for unit testing in ColdFusion because ColdFusion was primarily a presentation language that lacked a sophisticated ability to put together software components. With the introduction of ColdFusion components, however, a unit testing framework not only became extremely useful, but also was practical to implement and use.

Developers who don't like unit testing code usually argue that it's a waste of time. In other words, for every component you write, you end up having to write a test component along with it, which adds time to the development process. Developers who have been writing unit testing code for a long time, however, counter that the time you save in tracking down and fixing low-level bugs is well worth the relatively small amount of time it takes to write unit tests.

Unit testing frameworks like CFUnit make the process of writing unit testing code as efficient as possible. Some developers go so far as to write unit testing code before implementing the components that the tests are actually designed to test. Unit testing code is a good way to exercise your API and make sure it's usable and practical.

Credits: Christian Cantrell