Thursday, January 14, 2010

Upgrading to FlexUnit 4 for larger projects using FlexUnit .9

FlexUnit 4 is a much more powerful solution to unittesting in Flex than flexunit .9 was, but the documentation is a little sparse for right now. I've found over an over again the promise that it's backwards compatible with .9, and only one place that showed an example. Unfortunately, the code in that solution doesn't really work for larger projects that have many test classes. It took me a minute to realize that FlexUnit 4 doesn't need to be told specifically how to run a given test case, it will take a look at it and try to guess whats needed. The solution, then, because quite simple. Build a Suite like so:

// Metadata tells FlexUnit4 that this is a Suite and
// specifies a runner
to use with it
[Suite]
[RunWith("org.flexunit.runners.Suite")]
public class FlexUnit4TestSuite
{
//simply make public variables inside the suite
//with the test cases or suites that you want
//run.
public var baseMediatorTest:BaseMediatorTest;
// ...
}
Then you pass this suite into the test runner like so:

public function onCreationComplete(evt:Event):void
{
core = new FlexUnitCore();
core.addListener(new UIListener(uiListener));
core.addListener(new CIListener(1028, "localhost"));
core.run( FlexUnit4TestSuite )
}

Much more maintainable.