Avg. Rating 3.6

Problem

How to create an application that can be used to run and graphically view the results of FlexUnit tests.

Solution

Create an application that uses a TestSuite instance and TestRunnerBase component to run the tests.

Detailed explanation

This recipe extends the Create an application that can use the FlexUnit framework recipe.

1. Create an Application that includes TestRunnerBase

TestRunnerBase is the default graphical test runner included with the FlexUnit framework.

Edit the main application MXML file and change the contents as follows:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:flexui="flexunit.flexui.*">
    <flexui:TestRunnerBase id="testRunner" width="100%" height="100%"/>
</mx:Application>
At this point compile and run the application. The output should look like this:



2. Create a TestSuite

A TestSuite holds a collection of tests that should be run. This example does not add any tests to the TestSuite instance, it just creates the object that TestCase instances can be added to.

In the same MXML file add a Script block with the following code.
    <mx:Script>
        <![CDATA[
            import flexunit.framework.TestSuite;

            private function createTestSuite():TestSuite
            {
                var testSuite:TestSuite = new TestSuite();
                return testSuite;
            }
        ]]>
    </mx:Script>

3. Assign the TestSuite instance to the TestRunnerBase instance and start the tests

This assigns the TestSuite instance to the TestRunnerBase instance and starts running the tests. Add code for a handleCreationComplete function to the existing Script block:
            private function handleCreationComplete():void
           {
                testRunner.test = createTestSuite();
                testRunner.startTest();
            }
This will start the tests automatically after the application has loaded. Add a creationComplete handler to the Application that calls handleCreationComplete():
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:flexui="flexunit.flexui.*" creationComplete="handleCreationComplete();">
At this point compile and run the application. The output should look the same as step 1 above.

TestCase instances can now be added to the TestSuite instance and will be run when the application starts. See Adding a TestCase to a TestSuite and Understanding FlexUnit Output recipes.


Reference

The final MXML file:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:flexui="flexunit.flexui.*" creationComplete="handleCreationComplete();">
    <mx:Script>
        <![CDATA[
            import flexunit.framework.TestSuite;

            private function createTestSuite():TestSuite
            {
                var testSuite:TestSuite = new TestSuite();
                return testSuite;
            }

            private function handleCreationComplete():void
            {
                testRunner.test = createTestSuite();
                testRunner.startTest();
            }
        ]]>
    </mx:Script>
    <flexui:TestRunnerBase id="testRunner" width="100%" height="100%"/>
</mx:Application>
Report abuse

Related recipes