Test Driven Development is an essential part of any software project. Leveraging Flex Unit in your Flex and AIR projects is a well known best practice and will inevitably result in less unknowns and provide a much more reliable codebase, however like most things in life that provide value, unit testing comes at a cost: Time.
In general, writing Unit tests is often an overlooked process as it ultimately equates to an additional level of effort (time) in a project plan. However, writing unit tests should not be over looked for a number of reasons, and in my opinion most importantly because it forces developers to understand what problems the code they write are intended to solve, thus resulting in a better understanding of the problem domain.
Now without getting into a lengthy discussion about the benefits of Test Driven Development, as there is plenty of information available on the subject, I am going to focus on how to make it easier in Flex.
Typically when I write a unit test I stub it out and then run the test runner to make sure it fails. I pretty much do this immediately after I write the test and then I see that my test did not come up and realize I never added it to the test suite. Obviously something like adding tests to a test suite can be an automated process which would allow unit tests to be developed a little faster as all that would be needed was to write the test.
So in an effort to simplify the process of adding tests to a test suite I have developed a TestSuiteHelper which is a simple utility class that automates the process of adding unit tests to a test suite. Rather than manually adding each test the TestSuiteHelper will automate the process simply by invoking a static method and passing in the test class from which you need to extract all of the tests.
So if you would like to simplify your team’s unit testing workflow feel free to utilize the TestSuiteHelper.
hi eric,
thanks for the post, i’m always glad someone promotes testing since it helps immensely. making tests may seem to take more time, but they actually can reduce the amount of time allowing you to bypass having to run your whole app to get to the possible point of failure manually. instead you can isolate that point via tests. also, i find them helpful in debugging with the step-through debugger; i only have to run my tests in order to step through a specific section of code and i can avoid bringing up the whole app.
flexunit already has your testsuitehelper functionality built in, tho. if you extend TestCase and pass the TestCase class (not instance) as a parameter to a addTestSuite then it will automatically look for methods that begin with “test”.
here’s a quick example:
var suite:TestSuite = new TestSuite();
suite.addTestSuite(ManifestParserTest);
that call will add all “testXXX” methods as tests. this is in the isTestMethod of TestCase.
now if i could just automate the addTestSuite by introspecting over a package for all classes that extend TestCase, that would be sweet!
Hey Robert,
The TestSuiteHelper is different than the built in Flex Unit utility in one very important way; there are no rules enforcing a specific naming convention. Many developers use different nomenclatures for naming their tests, Flex Unit requires all tests to be prepended with the word “test” which is not very flexible
– Eric
Eric,
Have you seen digital primates dpUInt Flex unit and integration testing framework? I’ve done some work with it and I’m very, very impressed. They tried to use FlexUnit for unit testing UIComponents, but FlexUnit really doesn’t have the infrastructure to proper host UIComponents or asynchronous constructs to listen for events. I’ve done a presentation at a healthcare startup company in Minneapolis, MN where I incrementally built up a data entry form, driving the development with dpUInt tests. I was able to also integration test Cairngorm plumbing, ensuring that the full stack, including the HTTPService calls to a Grails server, worked appropriately. Again, I was very impressed with abilities of dpUInt.
http://code.google.com/p/dpuint/
Kind regards,
— chris bartling —
Hey Chris,
dpUnit is great, in fact, my team is incorporating dpUnit into our unit testing strategy. I particularly like how it allows the UI testing to be automated, as well as the ability to test events and service invocations.
With that being said, this post is really only intended to provide a utility for FlexUnit and not necessarily get into a discussion of Unit testing Frameworks.
Thanks,
Eric