Test Driven Development with ColdFusion Part I : An Introduction to Unit Testing
by Robert Blackburn
A father and son sat down to spend some quality time together, putting together a jigsaw puzzle. They cut open the box and began separating the 1,000 little cardboard pieces into groups. After some hard work, they had grouped all the pieces and were ready to start piecing the puzzle together. Scratching his head, the son asked, "So, where's the picture?" The father gave a little chuckle. "Why do we need a picture, son?" he asked. "We already know it's a painting of a forest. We'll just start fitting the pieces together, and when we are done we'll have a look at the picture to see if we got it right." The son was a bit concerned, but trusting his father, he began assembling the puzzle. They labored all day, and as you might imagine, they did not make much progress.
It may seem silly to try to solve a jigsaw puzzle without looking at the cover art until you are done, but this is the very mistake that many ColdFusion developers make when they build an application. They may do some basic functional tests during development, but they don't start testing until the very end of the development process. This is the same as working on a puzzle without a picture. Even if you know what your application is supposed to do through your use cases and other design documentation, you do not have a strict standard by which to measure your progress against.
Test Driven Development (TDD) is a methodology used to achieve this standard. In TDD the developer frequently runs "unit tests" to monitor his progress and uncover any newly-introduced bug.
What is unit testing?
Just as the picture on the cover of a puzzle defines how the puzzle will look when it's finished, a unit testing framework is a definition of how the application will work when it is completed. TDD and unit tests are not new topics and are common practices in many programming languages. In TDD, a developer begins by defining how he expects his code to work, and then writes unit tests based on those expectations. The developer does not write any code until the tests are written. Once the tests are completed the developer will frequently run them as he builds an application to receive instant feedback on the code's current behavior and progress. This can be a very different paradigm for many ColdFusion developers, who are often not accustomed to testing during the development process.
Why unit test?
Save time... eventually
I'm not going to lie. Writing unit tests will be slow at first, and will require an initial investment in time at the beginning of your development process. Like any investment, you will reap the benefits down the road, with interest. As a manager, I am very conscious of time management. I support unit tests and TDD because I know that even though it may cost me a few hours up front, I will gain three or four times that down the road.
Prevent the "House of Cards" Application
We've all seen it, a legacy application written by someone long ago. The application continues to function, but is becoming more and more outdated. Yet no one wants to touch the thing because if you change one thing, there is no telling what it might break. It's a "House of Cards" application; if anyone touches it, it will most likely come crashing down.
However, if the original developer(s) had been kind enough to write a unit test framework for the application, then someone would be able to modify the application, and through the unit tests be reasonably sure that they did not cause some unexpected side effect.
Prevent the home stretch bollix
It's been three months and the project is 90% done. The development team has enough of the application completed that they can begin to see it function from end-to-end, and they start testing. This flushes out all the little hidden bugs that have been accumulating over the last three months. So, it takes another three months to finish the last 10%.
If the developers had been using unit tests and TDD, they would have known about those bugs when they where first created, and they would have resolved them quickly because they would know what caused them (the code they just wrote).
Highlight design flaws early
Joe developer is developing on the model and control objects of an application while Mary developer is developing the view elements. They were smart enough to thoroughly design and document their application, so they each know exactly what the other will be building.
However, Mary needs to wrap up an earlier project before she can begin. That's okay with Joe and he begins his work. A few months down the road, Mary dives into her side of the application, and quickly finds that there are a few design flaws and she needs to make a few changes to the design.
Luckily for Mary, Joe already saw those design flaws, because when he was writing his unit test he saw that there were a few gaps and already compensated for them.
What is unit testing not?
Okay, enough of my rambling on about the glories of unit testing. Let's bring things back to Earth with what unit testing will not do for you.
First, unit testing is not a silver bullet bug finder. It will tell you when your application does not behave as you have defined it to behave. For the most part, the unit tests are only as thorough as the person who wrote them. Even with a perfectly written unit test, it is not a replacement for a human being testing the application by hand.
Second, unit tests are not a design supplement. They can definitely help with the design of an application, but are not a replacement for solid UML diagrams or other design techniques.
How do I get started?
TDD is a style of development, so the best way to follow TDD with ColdFusion can differ, depending on who you ask. However, there are a number of frameworks that can streamline the TDD process for you and help you get started. The two most popular frameworks for ColdFusion are CFUnit and CFCUnit.
Both are based in the xUnit style of TDD, and both are free open source projects. There are some slight differences in how they work, so try them both out to determine which works best for your needs.
In parts
II And
III of this article, I will walk you through building a unit test with CFUnit and how to integrate your tests into CFEclipse using CFUnit-Ant.
If you liked this article, check out "Test Driven Development and ColdFusion - From the Mouth of a .NET Developer!" by Judith Dinowitz.
Robert Blackburn is a developer and team leader of the Internet Application Development team at American Power Conversion Corporation (
http://www.apcc.com). He has been using ColdFusion since 1999, and has revived and manages the CFUnit open source project (
http://cfunit.sourceforge.net). He currently has a blog at
http://www.rbdev.net/devblog for his occasional ramblings.