FlexUnit Automation kit

This page describes my FlexUnit Automation kit, which makes it possible to run FlexUnit tests as part of an Ant build script. You can find the download link in the sidebar to the right.

The FlexUnit Automation kit uses Apollo Alpha 1, not AIR Beta 1, and has not yet been converted. It should be simple, but I have not had the time to do it, nor test it with AIR. The latest version (v1.1) is compatible with AIR 1.0 and later.

To use the kit you should be familiar with how to use the FlexUnit to test your ActionScript 3.0 code. If you are not, read Darron Schall's How to use FlexUnit with FlexBuilder 2 before trying the tricks described here.

Usage

Instead of the MXML described in Darron Schall's article, you need to use this template:

<WindowedApplication xmlns="http://www.adobe.com/2006/mxml"
                  xmlns:ix="net.iconara.testing.flexunit.*"
          creationComplete="testRunner.start()"
>

<ix:TestRunner id="testRunner"
             test="{createSuite()}"
   exitOnComplete="true"
/>

  <mx:Script>
  <![CDATA[
    import flexunit.framework.TestSuite;

    // import your test cases here
    import my.package.MyTestCase;


    private function createSuite( ) : TestSuite {
      var suite : TestSuite = new TestSuite();

      // add all you test cases to the suite
      suite.addTest(new TestSuite(MyTestCase));

      return suite;
    }
  ]]>
  </mx:Script>

</mx:WindowedApplication>
						

Import your test cases and add them to the suite in the createSuite method.

As you may have noticed, the MXML above describes an AIR application. The reason for this is that to integrate it into our Ant script we need to be able to run something, and it needs to be able to communicate back. An AIR application fits the bill.

Instead of the FlexUnit graphical test runner you may use today, the test runner in the code above doesn't have a user interface. Instead it writes the results to a file, which the Ant script echoes to the screen. To use this test runner make sure the compiler knows about the flexunitautomation.swc library (from the project zip file, see above right).

In your Ant build script create a new target like this:

<target name="run-tests" depends="compile-tests">
  <flexunit swf="${build.home}/tests.swf" failonerror="true"/>
</target>

The compile-tests target (you may call this whatever you like) that this target depends on should compile the MXML above into a SWF with the same name as the swf attribute of the flexunit task (but you will have to implement this target for yourself, it is beyond the scope of this kit). The flexunit task will run the SWF using adl (if you are on Windows you might need to edit the airexec.ant.xml and add .exe to the path to adl).

When this target runs it will print a log of the tests to the build log. If there are any failures or warnings these will appear more or less as they do in the graphical test runner.

The failonerror attribute is optional and defaults to false, if set to true the build will fail if the tests fail. There is also a logfile property that you can use if, for some reason, the default test.log doesn't suit you.

You may have noticed that the target above uses a task called flexunit. This is a Ant macro defined in the flexunit.ant.xml file (included in the project zip file). In order to use this you first need to include it, and another file which it depends on into your Ant build file. Put this at the top, somewhere after the <project> tag, but before any tasks:

<import file="airexec.ant.xml"/>
<import file="flexunit.ant.xml"/>

That about sums it up. You should be able to run the run-tests target now and see the test report.

Ideally there would be no extra dependencies like the custom test runner, but until I figure out something clever you will have to live with that. The flexunit macro could perhaps be wrapped up in a proper task, but it works for now. Check back from time to time and there may be an update.

Changelog

Dates are on the format YYYY-MM-DD.

2008-07-02
  • Updated for AIR 1.0, thanks to Espen Dalløkken
2007-05-16
  • Removed the dir attribute and changed the swf attribute to include the full path to the SWF, the task now figures out the directory part for itself.
  • Added an optional failonerror attribute which makes the task, surprise surprise, fail if the tests report an error
2007-05-15
  • Initial version