Flex Unit Testing
Unit testing rocks. It makes big projects less buggy, takes costs down, and in general is best practice for RIA development. On top of all that, the hours spent searching through thousands of lines of code to eliminate a simple error all but goes away! In the end testing is like source control. You don't know how much you need until you start using it.
So what is unit testing?
"It's about figuring out what you are trying to do before you run off to try to do it. You write a specification that nails down a small aspect of behavior in a concise, unambiguous, and executable form. It's that simple. Does that mean you write tests? No. It means you write specifications of what your code will have to do. It means you specify the behavior of your code ahead of time. But not far ahead of time. In fact, just before you write the code is best because that's when you have as much information at hand as you will up to that point."
Who uses it?
“The fact of the matter is, not only is Adobe Consulting leveraging FlexUnit each and every day on our projects, but we continue to be advocates to our customers of using FlexUnit on their projects.”
Steven Webster
Adobe Consulting
cairngorm framework core contributor
What are the benefits of using TDD?
- Faster overall development
- Less expensive (development and maintenance cycles)
- Less complicated code bases
- More independent and reusable code
- Less time and problems with regression testing
- Break new ground
What are some of the cons of TDD?
- Developer learning curve
- Slower initial development speed
- Problems with a MVC type framework (Nimbus, Cairngorm, PureMVC)
- Poor Documentation
Implementation
Ok, so by now you should be convinced that Unit Testing will save you time, money, and sleepless nights. But does FlexUnit play well with existing frameworks like Nimbus, Cairngorm, and PureMVC? In short. Not really. FlexUnit can do it, but in a hackish way. Adobe is trying to fix this though. They are integrating FlexUnit into the next release of Flex, Flex 4! Flex 4's release date is sometime in the 4th quater of 2009.
Until then, what do we do? Wait for more docs from Adobe! Alex Uhlmann, a really nice guy who also happens to be a Flex Ninja at Adobe consulting responded to an email saying:
Yes I and many others within AC use TDD. I understand your frustration with the lack of information out there. I've talked at the last MAX conference about it and AFAIK this will be on Adobe TV soon. I'll ask when exactly. Also, I plan on publishing a lot more around this area. [...] I've sent some emails to Adobe folks to find out when my video will be released.
Current Course of Action
In lieu of Flex 4 and better integrated Unit Testing in the Flex Builder, what should we do? Keep making your code testable wherever possible! In projects that are leveraging frameworks like Nimbus or Cairngorm most of the business logic will be executed in the Command classes. But that does not mean that the business logic of an application should reside in the commands themselves. There are many views on where business logic should reside (commands, model, helpers), but for making testable and getting more code coverage, the best practice for now will most likely be putting as much code into self contained utility classes and objects. This makes for better testing, and gets more code coverage!