How can I run tests with FlexUnitCore in Flash Builder Premium?

There are situations in which you will need to run tests using the FlexUnitCore directly. This class typically provides a bit more exposure for test operation. Without going into the reasons why someone would do this, this FAQ will merely walk through 'how' to do it.

This FAQ is made for users on Flash Builder Premium. We recommend that you read the Getting Started, and then you will have the necessary information to go through this FAQ.

Users on Flash Builder 4 Standard, Flex Builder 3, and Flex 4 SDK already use the FlexUnitCore class to run their tests.

A default FlexUnitApplication.mxml file will contain an onCreationComplete() method that looks something like the following:

private function onCreationComplete():void
{
    testRunner.runWithFlexUnit4Runner(currentRunTestSuite(), "Sample Project");
}

The testRunner id refers to the fairly simple UI testRunner that communicates with a browser UI and Flash Builder:

<flexui:FlexUnitTestRunnerUI id="testRunner">
</flexui:FlexUnitTestRunnerUI>

This class is Flash Builder's built in solution to pass the desired test cases to FlexUnitCore while displaying output in Flash Builder and a simplistic browser window. Unfortunately, this class is minimally exposed, and hard to use when calling the core.run() method directly.

Because there are situations in which you will desire to run your own FlexUnitCore, there is often a precedent to bypass the FlexUnitTestRunnerUI altogether.

Declare a private variable named core of type FlexUnitCore within the <fx:Script> block of the FlexUnitApplication.mxml file. Make sure to import the org.flexunit.runner.FlexUnitCore class.

private var core:FlexUnitCore;

Next add the following the <fx:Declarations> block:

<flexui:FlexUnitTestRunner id="testRunner">
</flexui:FlexUnitTestRunner>

As you can see, this MXML looks very similar to the FlexUnitTestRunnerUI. This class is instead added to the <fx:Declarations> block, because it performs the same function but does not provide a UI.

Next you will add a new UIListener:

Replace:

With

<adobe:TestRunnerBase id="testRunnerBase" width="100%" height="100%"/>

You will need to make sure that you have defined the namespace so that xmlns:adobe="http://www.adobe.com/2009/flexUnitUIRunner"

This new runner is a graphical runner that shows FlexUnit results in the browser window with greater detail. This is one of the two options for reporting from FlexUnitCore.

The core variable needs to be instantiated and aware of the new UIListener. Modify the onCreationComplete() method so that it reads as follows:

private function onCreationComplete():void
{
    core = new FlexUnitCore();
    core.addListener( new UIListener( testRunnerBase ) );

    core.run( currentRunTestSuite() );
}

From this point, you are able to run your tests in the browser, and receive results through the testRunnerBase. The UIListener displays as follows:

UIListener.PNG

However, this has not yet taken advantage of the best kind of test result reporting for Flash Builder Premium. In this case, we want our test results to display in the FlexUnit Results tab of Flash Builder. Modify the onCreationComplete() method so that it reads as follows:

private function onCreationComplete():void
{
    core = new FlexUnitCore();
    core.addListener( new UIListener( testRunnerBase ) );
    core.addListener( new XMLListener( "testRunner" ) );

    core.run( currentRunTestSuite() );
}

This sets up the testRunner as the XMLListener for the core. This ends up sending the core's results to Flash Builder, and allows Flash Builder to display results of tests run using core.run(). These results will display as follows:

twoCasesFlexUnitResults.PNG