« Cairngorm 2 Flex Library Project | Main | New FlexUnit Ant Task »
June 01, 2006
FlexUnit + Ant
I have been planning this blog for some time, but was spurred on by a recent blog on Continous Integration in Flex. This blog concentrates on integration between Ant and FlexUnit, I will post a follow-up entry with details on how to setup a CruiseControl server for Continuous Integration.
This blog entry was written against Flex 2.0 Beta 3.
I have seen various postings about integration between FlexUnit with Ant, however most solutions seem to require a Flex server. My motivation here was to create an Ant task that has no dependency on a server. That would allow unit tests to be run in autonomously.
Very early on I decided if I was running the tests from Ant I wanted to re-use the JUnit tasks that already ship with Ant. In particular I wanted to use the JUnitReport task, which merges the XML files generated by the JUnit task to produce an HTML report. Therefore I wanted FlexUnit to print the results in the same XML format as the XML formatter used by the JUnit task.
The question was then how to make the XML test results available to the Ant tasks, my answer was to use XML sockets.
Technical Design
My solution is comprised of a controlling Ant task, flexunit, and a FlexUnit test runner, JUnitTestRunner, which is shown in the diagram below. The flexunit task starts a socket server running inside of a thread and launches the Flash Player, which runs the tests using the JUnitTestRunner. When JUnitTestRunner has finished running the test it formats the results as per the JUnit XML format and sends them to the flexunit task over an XML Socket, the flexunit task then saves them to disk. We can then use the JUnitReport task to create a report or use CruiseControl to create a report.

Requirements
Software Installation
- Extract the Ant distribution.
- Add <your_ant_directory>\bin to your path.
- Extract the FlexUnitExample project to your Eclipse/FB workspace (you only need to extract it into your workspace if you want to load the project into Eclipse/FB - select File > Import and follow the Existing Projects into Workspace wizard).
- Edit <your_FlexUnitExample_directory>\build.properties and change the value of the flex.sdk.home property to the location of your Flex2 SDK installation (remember to use forward slashes).
- The SWF containing your tests must run in the local trusted sandbox, copy FlexUnitExample.cfg from <your_FlexUnitExample_directory> to C:\Documents and Settings\<your_username>\Application Data\Macromedia\Flash Player\#Security\FlashPlayerTrust.
- Edit FlexUnitExample.cfg so it has the path to <your_FlexUnitExample_directory>\bin.
Example
To run the build script open a command prompt and change directory to <your_FlexUnitExample_directory>. To run the build script execute the following command: ant.
This will execute the Ant build script, which will run the SWF containing the tests and generate a JUnitReport report. The reports directory under <your_FlexUnitExample_directory> will contain the XML files created by JUnitTestRunner, you can view the report by opening <your_FlexUnitExample_directory>\reports\html\index.html. You should see two tests, one that passed and the other that failed.
Resources
You can download the source code for flexunit and JUnitTestRunner below, they are provided as an Eclipse Java Project and an Eclipse Flex Library project respectively.
- Download the source for the flexunit Ant task (FlexAntTasks).
- Download the source for the JUnitTestRunner (FlexUnitOptional).
You can import these projects into your workspace – extract the files into your workspace and in Eclipse select File > Import and follow the Existing Projects into Workspace wizard.
Posted by at June 1, 2006 02:57 PM
Comments
We are planning to use XMLSocket server in our own fUnit testing framework to integrate our framework with Ant.
One main advantage of our own fUnit testing framework is support of mock testing of Business Delegates.
Great to see that we are thinking in the the same way :)
Posted by: JabbyPanda at June 2, 2006 10:20 AM
Peter,
This is great. This is a problem I've been considering for a while, but now that I've knuckled down to solve it, a quick search finds that you've solved it for me.
The only improvement I could see is that the ant task fail the build on a flex unit test failure/error. I'll look into how I would implement that and perhaps I could share it with you.
Posted by: Tony Hillerson at June 22, 2006 11:55 PM
Hi,
try to use flexunit but got an error during compile time.
Error: Type was not found or was not a compile-time constant: Timer
Thanks in advance.
Posted by: dougi at July 4, 2006 11:47 AM
Sorry, it's ok now...take the last package version.
Thanks
Posted by: dougi at July 4, 2006 02:05 PM
For those trying to run this on the final Flex 2.0 version:
In the build.xml, you need to prep-end the path of the SWF to with ${basedir}. Otherwise the server socket will start to listen but the SWF will never actually start. This will leave your script idling without any errors being reported.
I would add this line to beginning of the FlexUnitLauncher.runTests method:
if(!new File(swf).exists()) {
throw new FileNotFoundException("Could not locate: "+swf);
}
Posted by: Mateo Barraza at July 19, 2006 09:01 PM
In order to get this running on the final version of Flex 2, I had to make some additional changes.
1) As noted above, edited build.xml to add the attribute basedir="." to the root node and modified the swf attribute for the flexunit task to be swf="${basedir}/bin/AntTestRunner.swf"
2) Replaced the flexunit.swc file in the example download with the latest flexunit.swc file available from adobe labs http://labs.adobe.com/wiki/index.php/ActionScript_3:resources:apis:libraries#FlexUnit
3) Changed the windows files association for .swf files from flash player 8 to flash player 9.
Then it worked great. Peter, thanks for the example project. It was exactly what I was looking for.
Posted by: Jon Marston at October 11, 2006 11:10 PM
The following points should help users to get the tests and reports running with Flex Builder 2.0.1:
1. Download and import both the FlexUnitExample and FlexAntTasks projects into Flex Builder.
2. When editing the build.properties file, use the Eclipse Text Editor and not the Build Properties Editor.
3. Ignore the step regarding the local trusted sandbox. The sandbox settings seem to be updated automatically by Flex Builder.
4. Copy the FlexAntTask.jar from the dist directory of the FlexAntTasks project into the lib directory of your Ant distribution.
5. Edit the build.xml file of the FlexUnitExample project and make the following changes:
- Change the haltonfailure attribute of the flexunit task to false.
- Uncomment the junitreport task.
Posted by: Tom Sugden at February 1, 2007 02:03 PM
Hi,
How would I know from FlexUnitExamle what assertion line gives a failure?
Posted by: Andrey at February 12, 2007 05:58 PM
Hi there,
Well just to answerd the last post's question, if you navigate through this
FlexUnitExample\report\html\index.html
you will notice that you can click on the left side to check the assertions from your classes.
Anyways, just to clarify, the files that you have to download are FlexAntTasks-src.zip(be careful, there is also a FlexAntTasks.zip that can not be imported in FlexBuilder) and FlexUnitExample.zip. FYI. you can find the newest version, here:
http://weblogs.macromedia.com/pmartin/archives/2007/01/flexunit_for_an_1.cfm
Posted by: WilliamX at February 20, 2007 05:17 PM
Hi
I have written build.xml to checkout the code from CVS.so its working fine.but the problem is ,in CVS my project "customs"is under "framework" folder.Like framework/customs.so when i checkout the code i am getting entire folder structure,i.e framework/customs.so,i want only "customs" to be checkout.
Thanks
Radhika.P
Posted by: Radhika at April 17, 2007 07:29 AM
Hi Peter,
When I run flexunit tests with the Ant task, it seems that on Windows the standalone player SAFlashPlayer.exe is launched. However, this is only available as part of Flex Builder (I couldn't find a download for just the standalone Flash Player).
Can you confirm that Flex Builder is required to run tests with the flexunit Ant task?
Thanks,
Manish
Posted by: Manish Shah at April 25, 2007 09:25 AM
It seems there's no way to detect if errors were thrown by any unit tests? Looking through the source there's handling of errors for the log report rendering, but not for the test report or "exporting" properties back to Ant for conditional processing. That seems odd?
Posted by: Barney at May 1, 2007 10:56 PM
Hi Peter,
Do you have a view on this issue? FlexUnit ant task will fail the build if there is a failure, but not if there is an error. Do you think it should?
Cheers,
Manish
Posted by: Manish Shah at May 11, 2007 01:04 PM
Hi Peter,
I have been using the flexunit ant tasks and I have encountered a problem:----->>>>>>>>> [flexunit] opened server socket
[flexunit] Exception in thread "Thread-37" timeout waiting for flexunit report
[flexunit] at com.adobe.ac.ant.tasks.FlexUnitTask$1.run(Unknown Source)
[flexunit] Caused by: java.net.SocketTimeoutException: Accept timed out
[flexunit] at java.net.PlainSocketImpl.socketAccept(Native Method)
[flexunit] at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:384)
[flexunit] at java.net.ServerSocket.implAccept(ServerSocket.java:450)
[flexunit] at java.net.ServerSocket.accept(ServerSocket.java:421)
[flexunit] at com.adobe.ac.ant.tasks.FlexUnitTask$1.openClientSocket(Unknown Source)
[flexunit] ... 1 more
[flexunit] --- Nested Exception ---
[flexunit] java.net.SocketTimeoutException: Accept timed out
[flexunit] at java.net.PlainSocketImpl.socketAccept(Native Method)
[flexunit] at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:384)
[flexunit] at java.net.ServerSocket.implAccept(ServerSocket.java:450)
[flexunit] at java.net.ServerSocket.accept(ServerSocket.java:421)
[flexunit] at com.adobe.ac.ant.tasks.FlexUnitTask$1.openClientSocket(Unknown Source)
[flexunit] at com.adobe.ac.ant.tasks.FlexUnitTask$1.run(Unknown Source) Can you please tell me why this happens and how I can deal with it. Many thanks and have a great new Year, Avier
Posted by: Avier5 at December 31, 2007 10:59 AM
Hi,
I cant find the FlexUnitExample.cfg file in the sample zip file... has it been removed?
Posted by: Mark Fletcher at January 9, 2008 09:54 PM
Actually Im getting this error now when I try to run the AntTestRunner.mxml app:
Error #2044: Unhandled IOErrorEvent:. text=Error #2031: Socket Error.
at flexunit.junit::JUnitTestRunner/::sendResults()
at flexunit.junit::JUnitTestRunner/endTest()
at flexunit.framework::TestResult/endTest()
at flexunit.framework::TestResult/::doFinish()
at flexunit.framework::TestResult/::doContinue()
at flexunit.framework::TestResult/::doRun()
at flexunit.framework::TestResult/run()
at flexunit.framework::TestCase/runWithResult()
at flexunit.framework::TestSuite/::runTest()
at flexunit.framework::TestSuite/runNext()
at flexunit.framework::TestSuiteTestListener/handleTimer()
at flash.utils::Timer/flash.utils:Timer::_timerDispatch()
at flash.utils::Timer/flash.utils:Timer::tick()
Any ideas on how to resolve this?
Posted by: Mark Fletcher at January 9, 2008 10:05 PM
I cannot find the FlexUnitExample.cfg file anywhere. Can anyone point me to it?
Posted by: Scott at February 14, 2008 03:58 AM
What license will FlexAntTask use?
Posted by: David Sun at February 14, 2008 07:31 PM
I'm having a problem running this. I got as far as the test runner runs the tests, but it never closes or generates the report. The last thing ant reports is: [flexunit] opened server socket.
I was expecting, but never got something like:
[flexunit] accepting data from client
[flexunit] sent policy file
[flexunit] accepting data from client
I also never found a .cfg file in the project. So, I'm wondering if this is a security issue.
Any suggestions on how to get this to work from here?
Posted by: JP at July 21, 2008 10:58 PM
As you say you should expect to see "sent policy file", etc. However, you shouldn't need a .cfg file as the server-socket send providing the policy as runtime.
The only time you will need a .cfg file is if your tests are making their own network requests e.g. I have seen tests that try and load an XML file.
From what you are saying though the task is launching the SWF and you see your tests executing OK? Have you changed the ports at all in your Ant file or test runner?
Posted by: Peter Martin at July 24, 2008 09:35 AM
Hi Peter,
I've downloaded the FlexUnitExample and FlexAntTasks-bin.zip, which contained FlexAntTasks.jar.
I'm not sure where to put the jar, in ant's lib directory or FlexUnitExample's directory.
I've downloaded the latest ant dist (apache-ant-1.7.1).
I've downloaded the latest JDK jdk1.6.0_07 as the ant build complained about not finding tools.jar and I have Flex Builder 3.
The ant output goes as far as "opened server socket" and then a long pause. Let me know what is wrong?:
Buildfile: build.xml
compile:
[exec] Loading configuration file C:\Program Files\Adobe\Flex Builder 3\sdks\3.0.0\frameworks\flex-config.xml
[exec] C:\data\Projects\FlexUnitExample/bin/AntTestRunner.swf (161025 bytes)
test:
[flexunit] opened server socket
Posted by: eeidfn at August 5, 2008 09:17 PM
With regards of where to put the JAR that is up to you. You can put it in the Ant lib folder. Personally I prefer to put it under my project, I like to have everything baselined in the source code repository. I then reference in my build script as follows:
It is difficult to say what is wrong. As we can see form the logs the server socket has been opened and it is waiting for a client connection / the test report. Do you see the SWF being launched?
Posted by: Peter Martin at August 10, 2008 05:04 PM
