Quick Tip: FlexUnit & ANT

Even if you have worked already with flexunit task in ant, it may not be obvious that there are 2 ways to execute it. First, you can compile runner.mxml to .swf prior to flexunit task or you can force flexunit task to generate/compile runner for you. Now lets have a look at pros and cons of those 2 solutions. Unfortunately it seems the flexunit ant task development has stopped sometime a year ago, and a few features are missing in order to call this task perfect, so it seems we are left in wild with what we have…

Running compiled runner.mxml

<flexunit toDir="..." workingDir="..." haltonfailure="true" verbose="true" localTrusted="true" swf="runner.swf"/>
  • simple one line definition
  • requires runner.swf to be compiled prior to the task (you can use mxmlc task)
  • your runner.mxml is a static file containing predefined test cases/suites, there is no simple way you can dynamicaly force test cases/suites to be tested

See more details for flexunit ant task in wiki.

Here is an example of a simple runner.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
	creationComplete="runTests();" layout="absolute">
			import org.flexunit.listeners.CIListener;
			import org.flexunit.listeners.UIListener;
			import org.flexunit.runner.FlexUnitCore;

			public function runTests() : void
				var core : FlexUnitCore = new FlexUnitCore();
				core.addListener(new UIListener(uiListener));
				core.addListener(new CIListener());
	<flexUnitUIRunner:TestRunnerBase id="uiListener" width="100%" height="100%"  />

Running dynamically generated runner

<flexunit toDir="..." workingDir="..." haltonfailure="true" verbose="true" localTrusted="true">
    <source dir="${basedir}/src" />
    <testSource dir="${basedir}/src">
        <include name="**/*Test.as" />
        <include name="**/*Test.mxml" />
    <library dir="${FLEX_HOME}/ant/lib">
        <include name="**/*.swc" />
  • lets you dynamically define test cases/suites to be included for testing (testSource)
  • lets you define libraries and sources to be included, but that is where it ends (source, library)
  • does not let you define additional compiler arguments (theme, flash player version, resource bundles etc.)

Make sure to define flexunit task in ant, if you do not have .jar file, just download the latest from flexunit.org or adobe.com, and do not forget to copy necessary .swc classes into your libs dir.

<taskdef resource="flexUnitTasks.tasks" classpath="${FLEX_HOME}/ant/lib/flexUnitTasks-4.1.0-8.jar"/>

JUnit reports

This task should be defined and available in ant, but in case it is not (you run ant from maven), just include taskdef and target .jar files (should be available with ant download package)

<taskdef name="junitreport" classname="org.apache.tools.ant.taskdefs.optional.junit.XMLResultAggregator">
        <pathelement location="${FLEX_HOME}/ant/lib/ant-junit.jar" />
        <pathelement location="${FLEX_HOME}/ant/lib/ant-junit-4.jar" />
<junitreport todir="target/surefire-reports">
    <fileset dir="...">
        <include name="TEST-*.xml"/>
    <report format="frames" todir="target/surefire-reports/html"/>

Where to go from here:

1 comment so far

  1. [...] Quick Tip: FlexUnit & ANT [...]

Leave a comment

Please be polite and on topic. Your e-mail will never be published.