You can run Ant via command line or from within Eclipse which provides an Ant panel if you use Flex Builder plugin version. If you are using Flex Builder standalone check this article about how to install Ant it afterwards.
You have to download Peter Martin’s JUnitTestRunner class which was build to test within the Flash Player and not AIR so it uses XMLSocket to hand over the test result. AIR has the capability to write directly to the users hard drive so we will add this functionality by subclassing JUnitTestRunner.
Download the AIRAntUnitTest project and import it into Flex Builder.
You will get four errors because Peter Martin’s JUnitTestRunner class is not delivered in my zip. So please download it from his blog (last Donwload link at the end of his post) and add it to the directory flexunit/junit.
You have to change the JUnitTestRunner class slightly to make my subclass work:
- Change the following member variables from private to protected: reports, totalTestCount, numTestsRun
- Change the createXMLReport method modifier from private to protected
Now the project should build without errors.
Change the build.xml and enter the path to your installed Flex SDK by defining the property flex.sdk.dir (in my case D:/tools/flex_sdk_220.127.116.113).
Your setup is complete and now you have two options to launch you unit tests:
The first option is to use the visual runner of the FlexUnit project. Right-click the AIRAntUnitTest.mxml and choose Run As->Adobe AIR Application. This launches the debug launcher and since no parameter is passed the visual runner get’s the test suite and is started (onInvoke event listener). The result should look like this:
The second option is to use Ant. If you want to launch it from within Eclipse open the Ant panel (Window->Show View->Other->Ant->Ant). Now drag the build.xml from the AIRAntUnitTest project into this panel and it should look like this:
Here you see the five different Ant targets. The build target is default and calls clean, compile, run-tests and generate-reports. Double-click “build” to run the tests.
- clean removes the bin-release and reports directory
- compile compiles the SWF for AIR
- run-tests opens the AIR application in the debug player (ADL) which get’s closed with exit code 0 if all tests passed and 1 if there were failures or errors
- generate-reports uses the junitreport feature of Ant and handles the xml results generated by Peter’s JUnitTestRunner and written to hard disk with the AIR file API
The generated reports look like this:
If you want your build to fail when failures or errors come up change the attribute failonerror to “true” in the run-tests target.
With this approach you could run your unit tests automatically on your build server and do Continous Integration so you would have a self-testing build.
Many thanks to Peter Martin for his JUnitTestRunner and I hope this AIR approach is usefull for some of you guys.