FDT and FlexUnit 4.1 Beta
19th June, 2010 – 5:26 pmFlexUnit 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:
{
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>
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.
<!– 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:
3 Responses to “FDT and FlexUnit 4.1 Beta”
Great article! Have you tried to use FDT4 and take advantage of the Project Templates?
By Bruno Fonzi on Jul 4, 2010