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.


  • Download the Ant distribution.
  • Download the example project (FlexUnitExample).

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.


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.


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.

27 Responses to FlexUnit + Ant

  1. JabbyPanda says:

    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 🙂

  2. 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.

  3. dougi says:

    Hi,try to use flexunit but got an error during compile time.Error: Type was not found or was not a compile-time constant: TimerThanks in advance.

  4. dougi says:

    Sorry, it’s ok now…take the last package version.Thanks

  5. Mateo Barraza says:

    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);}

  6. Jon Marston says:

    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#FlexUnit3) 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.

  7. Tom Sugden says:

    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.

  8. Andrey says:

    Hi,How would I know from FlexUnitExamle what assertion line gives a failure?

  9. WilliamX says:

    Hi there,Well just to answerd the last post’s question, if you navigate through thisFlexUnitExample\report\html\index.htmlyou 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

  10. Radhika says:

    HiI 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.ThanksRadhika.P

  11. Manish Shah says:

    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

  12. Barney says:

    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?

  13. Manish Shah says:

    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

  14. Avier5 says:

    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

  15. Mark Fletcher says:

    Hi,I cant find the FlexUnitExample.cfg file in the sample zip file… has it been removed?

  16. Mark Fletcher says:

    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?

  17. Scott says:

    I cannot find the FlexUnitExample.cfg file anywhere. Can anyone point me to it?

  18. David Sun says:

    What license will FlexAntTask use?

  19. JP says:

    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 clientI 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?

  20. Peter Martin says:

    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?

  21. eeidfn says:

    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.xmlcompile:[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

  22. Peter Martin says:

    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?

  23. Bob Weiss says:

    Hello Peter,Thanks for sharing this work. I have been able to setup & execute everything as documented via the ANT command line. I would like to run from within FlexBuilder (v3.0) for debugging but whenever I attempt this I receive the Error #2044: Unhandled IOErrorEvent: Text=Error #2031: Socket Error. Is there any reason I should not be able to run from with FlexBuilder? Any comments or suggestions would be appreciated.Thanks

  24. Peter Martin says:

    Hi Bob,What are you trying to debug? I can’t think of any reason why it wouldn’t work, but I haven’t tried. Normally if within Eclipse I just use the standard FlexUnit test runner, which allows me to debug my tests. Is there any reason you are trying to use the Ant test runner?CheersPeter.

  25. Ted Stevko says:

    This is just for reference; I’m not completely sure that this is clear, but since I was experiencing it, I figured I’d post it along with the rest of this just to make sure it’s out there.My application is currently loading a localized file as part of it’s configuration system. I encountered a problem that because it’s reading a local file, but Flex Unit + Ant wants to use a socket server. This created a local-vs.-network sandbox issue, and since the local file is loading first, it’s not allowing the socket to be opened.(BTW: when the unit tests weren’t working, I was also getting the “socket server opened” and a hang. Try using this with either Flash CS3 opening your .swf files, or with the Flash Debug Player. Both show more information, and will at least throw up players. If you’re already doing that, all I can say is that the tests aren’t finishing and the socket server isn’t closing.)The solution to this is to set your Flash Global Security Settings to allow the folder or swf to run anyway. Here’s the URL to the security settings:http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.htmlOnce this is set, you can run the application no problem.Hope this helps,Ted Stevko

  26. AJ says:

    How can I run a FLEX UI inside eclipse and have that UI interact with an eclipse based UI/plugin (e.g. data exchange, drag&drop, etc.)?Basically I want to be able to run my Flex app inside eclipse and have that app to be able to interact with eclipse and/or other eclipse plugins.Any help with this is greatly appreciated.Regards,

  27. Peter Martin says:

    I haven’t looked at this myself, but I know others have. It would be great to have FlexUnit running as an Eclipse plugin. The design view in FB embeds Flash (as far as I know) so you should look at the FB API if you haven’t done so already.