views:

2303

answers:

10

If you create a pure ActionScript project in Flex Builder 3 and want to do unit testing using flexunit, what is the best option?

The built-in Flex builder will refuse to build the mxml file containing the TestRunnerBase component as it is a pure ActionScript project (no Flex allowed). It is impossible to add the mxml file to the "ActionScript Applications" list in the project settings.

Right now I can see two options, both undesirable.

  1. Add the unit testing mxml file to the project and create an external tool setup to build and run it. This is the approach I'm taking now, and it works fine, except that interactive debugging is impossible.
  2. Create a new Flex project just for the test mxml file and add the main project's src directory as an additional source directory in the build options. I don't like this approach because it requires that I keep the mxml file in a separate directory tree from all the other source files in addition to the ugliness of maintaining two projects.
+2  A: 

There's always ASUnit.

Theo
+1  A: 

I ended up putting the unit test mxml file in the original project, creating a new Flex project, deleting the src directory, and replacing it with an Eclipse linked folder to the src directory of the ActionScript project. This setup seems to work fine.

Tmdean
+1  A: 

We've done something similar in order to get FlexUnit working with CruiseControl.net (continuous integration server).

In our implementation, we have the code below run in the FlexEvent.CREATION_COMPLETE handler of the Application class.

How you output the results of the unit tests is completely up to you. Our implementation has been used with both AIR and Zinc3 and both output an NUnit-friendly XML representation and then exit the application (with an exit code of -1 if any tests failed).

// import mx.core.Application;
// import flexunit.framework.*;

// class AutomatedTestHarness extends Application implements TestListener

private function creationCompleteHandler(event : Event) : void
{
    this._result = new TestResult();
    this._result.addListener(this);

    var testSuite : TestSuite = new TestSuite();
    this.addUnitTests(testSuite);

    testSuite.runWithResult(_result);
}

/**
  * Implement these as part of TestResult.addListener
  * If you want to output xml after the tests run, do so here
  * (Tip: Count tests in endTest and compare the count to testSuite.countTestCases()
  * to find out when all tests have completed)
  */
function startTest(test : Test) : void {}
function endTest(test : Test) : void {}
function addError(test : Test, error : Error) : void {}
function addFailure(test : Test, error : AssertionFailedError) : void {}
Richard Szalay
+1  A: 

Maybe you could use flexunit.textui.TestRunner, which output the result to the Console.

+1  A: 

We've factored out all the code we want to test into library projects. We then just have a separate project for our tests, which is a flex project, which depends on the project under test.

Scotty Allen
That sounds like it would work too, but so far I haven't had any problems with my linked folder setup.
Tmdean
A: 

Try AS3Unit from libspark. They also have an async beta test kit.

OK... these two (ryan/drawcoder) are very suspicious; please don't post under duplicate accounts.
Marc Gravell
A: 

Try AS3Unit from libspark. They also have an async beta test kit.

Ryan Christensen
OK... these two (ryan/drawcoder) are very suspicious; please don't post under duplicate accounts.
Marc Gravell
A: 

remove the 'excludedEntries' element in your project's .actionScriptProperties file should work, I use this way to build mxml file in my pure actionscript project.

Peng Qi
A: 

You can check out how we've set up the build for Robotlegs using FlexUnit4 and their CI ant tasks.

For version control we strip out all of the Flex/Flash Builder project files. src and test folders are both set up as src paths. Tests are rand via the ant build. Alternatively a second project with a runner can be set up if you life the visual test runner.

It has been very effective and easy to use across many contributors.

Joel Hooks
A: 

I have also tried your second option, however, I am having trouble referencing the external source. The intellisense can see the import and class name, but there's a compiler error telling me:

"Definition test:testsource could not be found.

Call to possibly undefined method testsource.

Type was not found or was not a compile-time constant:testsource.

I'm not sure what I'm missing

tom