What are Best Practices when using FlexUnit?
This question comes in from John, one of the regular attendees of the Flextras Friday Lunch. He asks me about unit testing and some best practices.
I have played with FlexUnit and done all the tutorials [I could find] and understand conceptually how unit testing works. What I've not been able to easily find out, for the newbie who is trying to get into TDD, do you keep your FlexUnit tests in your application project and develop classes around them that pass those tests or do you run a parallel project that runs unit tests. It would seem to me that the former would be true if you actually want to test each time you make a major change or add a class, but hell if I want to go down a long road to find out that nobody really puts their unit tests inside their real project.
I have gone through the exact same issues that John has had with unit testing and lack of documentation on the approaches. I have come up with no concrete answers and am not sure what folks would consider a best practice [or not]. But, I can tell ya'll what I did when writing unit tests for the Flextras Calendar.
I keep the unit tests in a separate project. Since most of my work is with creating components, the code I care about testing is already in a library project. From there, I just create an AIR project strictly for unit testing. I do it in AIR because then I don't have to set up a web server and/or directory for the new project.
The main application is just an instance of a runner, which loads a test suite, which has a bunch of test cases. The real testing magic is in the test cases, of course.
I cover this approach in The Flex Show's extended screencast series on building Flex Components, which is available for purchase.
Now, I understand that Flash Builder 4 has some built in unit testing features, including a view that will act as the test runner. I thought it also had the ability to keep tests in the same code as the project that you want to run tests against. I was never able to get it to work during the prerelease cycle and haven't tried since the official release. In fact, the unit testing that I have written were written against FlexUnit 1. At the time I was writing unit tests for the Flextras Calendar, that was the only unit testing framework I could find any documentation on.
I respect the fact that FlexUnit 4 probably has a lot more features, but having features and giving me the information I need to make use of them are two different things.
How do you guys use unit testing in your projects? Have you had luck with Flash Builder 4's features?
I'll add that one of the aspects of Test Driven Development is that you should write your tests first. In theory this is a great idea, but I don't think it is practical in the real world. Does anyone really do that? Does anyone really work in an environment were they have time to do that? A lot of the consulting I do is "come in and save the day" type of stuff. I'm rarely put in situations where I'd be writing tests first; and I've never come across a client who does it. Maybe the programmers who do don't need to hire folks to come in and save the day?
I love fielding questions from readers (and listeners). If you have one, a href="http://www.jeffryhouser.com/contact.cfm">Please ask. I can even take confidential questions over at www.asktheflexpert.com, or you can put me on the spot life at a Flextras Friday Lunch recording session.
Be sure to come visit me at the Flextras booth at 360Flex in DC this September. Order you tickets with this link and I get Karma points from the 360|Flex folks. If you ask real nicely I may be able to get you a discount code.





A lot has changes since the FlexUnit .9 release. And we have been working very hard on increasing the documentation and examples around the project. But just as a way to point people in the right direction you should always start your search for information on the FlexUnit site (http://www.flexunit.org/). From there you will get news and info about the project. From that site you can also link out to our Wiki (http://docs.flexunit.org/) that has quite a bit of information on FlexUnit and testing in general.
We are currently in the process of developing new content that includes not only information on setting up your application for testing but also features tips on writing better/more testable code in your projects.
Beyond the actual documentation we have an active mailing list on google groups as well as we are starting up a community knowledge base on TenderApp (https://flexunit.tenderapp.com) which is probably the best place to get individual questions answered.
Hopefully we will have more information on that available after the release of FlexUnit 4.1. Which will likley have its RC1 release this weekend.
Thanks,
sim