FlexUnit 4 was a massive leap forward for Test Driven ActionScript 3 Development allowing us to make use of metadata annotations, hamcrest and easier asynchronous testing and I was delighted to discover that the flexunit.org team are still busy working away preparing for the next milestone release of FlexUnit 4.1 which boasts some great new features, my favourite being an ANT Task to generate TestSuite’s automatically (something that only Antennae appeared to offer before).
This wiki post will show you how to get the FlexUnit 4.1 beta up and running in FDT 3.5 by making use of the FlexUnit ANT tasks.
First up we will download the FlexUnit 4.1 beta; I will be downloading the build for FlexSDK 3.5, AS3 Pure.
Once unzipped, you will want to add the FlexUnit 4.1 libraries to your project’s libs folder; once copied across your project should look something like the screenshot below:

Copying the FlexUnit 4.1 libs into your FDT project
Once you have copied the FlexUnit 4.1 libs into your project you will want to add the FlexUnit core library SWC to your FDT Class Path so you get code completion on FlexUnit methods; this is done by right-clicking on the flexunit-core-4.1.0-beta1.swc file; again see the screnshow below:

Adding the FlexUnit 4.1 Core SWC to your project's Class Path
Next up we will add a very simple, sample test to the project, start by adding a new “Source Folder” to your project by selecting File -> New -> Source Folder; name the folder test-src. Now create a new ActionScript Class in the test-src folder and name it TestExample.as.
Because FlexUnit 4 is driven by metadata, there is no need to extend a base class; instead you just need to annotate the methods which represent test with the [Test] metadata tag. Our example test will do nothing, but will at least allow FlexUnit 4.1 to fire up and show some output:
package { import org.flexunit.asserts.assertTrue; /** * An example Test Case to get FlexUnit 4.1 Beta up and running. * @author Jonny Reeves */ public class TestExample { [Test] public function exampleTest() : void { // Not much of a test! :) assertTrue("FlexUnit 4.1 Beta is running under FDT", true); } } }
The final step is to setup the ANT Build File which will automatically create our TestSuite and launch the FlexUnit 4 Visual Test Runner to display the outcome of our test. If you have not used ANT before in FDT then you may need to open the ANT View by selecting Window -> Show View -> Ant.
Create a new file named build.xml in your project; when you open this file for editing FDT should automatically fire up the ANT editor which provides syntax highlighting for you. Start off by creating a barebones ANT file:
<project name="FlexUnit4.1-Example"> </project>
Now drag the build.xml into the ANT View; once added it should appear like the screenshot below:

Creating an ANT BuildFile and adding it to the ANT View
Now we want to create a new Build Task which will generate a Test Suite from our test-src folder and then compile a FlexUnit 4.1 SWF which runs those tests.
<project name="FlexUnit4.1-Example"> <!-- Update this property to point to your FLEX SDK; note there is a bug in FlexUnit 4.1 Beta 1 where the FLEX SDK path must NOT include any spaces, otherwise you will get an error during the MXMLC compilation stage; this has already been fixed in the latest Builds of FlexUnit 4.1, see: http://bit.ly/dpHcHT --> <property name="FLEX_HOME" location="${eclipse.fdt.flexsdk}" /> <property name="bin.dir" value="${basedir}/bin" /> <property name="report.dir" value="${basedir}/reports" /> <property name="lib.dir" value="${basedir}/libs" /> <property name="test-src.dir" value="${basedir}/test-src" /> <taskdef resource="flexTasks.tasks" classpath="${FLEX_HOME}/ant/lib/flexTasks.jar" /> <taskdef resource="flexUnitTasks.tasks" classpath="${lib.dir}/flexUnitTasks-4.1.0-beta1.14.jar" /> <!-- Run FlexUnit 4.1 and generate JUnit Output --> <target name="test" depends="init" description="Run Unit Tests"> <flexunit workingDir="${bin.dir}" toDir="${report.dir}" verbose="true" haltonfailure="true"> <testSource dir="${test-src.dir}"> <include name="**/*Test*.as" /> </testSource> <library dir="${lib.dir}" /> </flexunit> <junitreport todir="${report.dir}"> <fileset dir="${report.dir}"> <include name="TEST-*.xml" /> </fileset> <report format="frames" todir="${report.dir}/html" /> </junitreport> </target> <!-- Create the required directories and clean out the reports folder --> <target name="init"> <delete dir="${report.dir}" /> <mkdir dir="${bin.dir}" /> <mkdir dir="${report.dir}" /> </target> </project>
Once you’re build file is setup you can execute the test ANT task. If the task completes succesfully, FlexUnit will have populated the reports folder with both JUnit style reports and an HTML version which can be opened in a web browser.
If the build fails then the Output Console view should provide you with some debugging hints; if you are using FlexUnit 4.1 Beta 1 then make sure your Flex SDK path does not include any spaces (this particular bug will be fixed in beta 2 of FlexUnit 4.1).
Further reading:
Pingback: Tweets that mention FDT and FlexUnit 4.1 Beta - JonnyReeves.co.uk -- Topsy.com
Pingback: Twitted by richtretola
Great article! Have you tried to use FDT4 and take advantage of the Project Templates?
Hi, Thank you for this great guide!
Using FDT i’m having some trouble using FP10 features. I modified the ANT build.xml file to use the libraries required (the ‘linked SWC’s” from FDT).
But when running the test task it is not able to find the class Vector, which is a FP10 feature.
Do you know why this is / how to fix this?
*EDIT* I’ve found a solution to use FP10. Had to change the target version in:
$FLEX_SDK/frameworksflex-config.xmlThanks Chris; glad you found the guide useful – I will update it once FlexUnit 4.1 goes final.
As for targeting Flash Player 10, whist modifying the flex-config.xml file will work, it’s not recommended as it will affect ALL flash applications which are compiled using it. Currently there is no way to target a different flash player version (or indeed any other configuration values) when using the FlexUnit ANT Tasks; there is a JIRA ticket open (and I believe it’s been implemented in the FlexUnit 4.2 dev branch). So if you want to keep using Flex SDK 3.5 you will need to compile your own TestRunner SWF.
Alternatively, if you can just compile against the Flex SDK 4 which will target flash player 10 by default!
Brilliant! Just what I was looking for, really usefull