This is a very technical post, so if you are not interested in Actionscript or Unit Testing, you can stop reading.
A few weeks ago I gave a talk to the Flash Coders NY group about Unit Testing. The talk was mainly oriented towards the benefits of unit testing, but since I’ve taken the time to set up an automatic test runner for the talk, I also took the time to clean it up, make it work for Actionscript2 and make it as easy to use out of the box as possible.
Hopefully this will lower the frustrations of all those that want to start looking into Actionscript Unit Testing but get turned off by all the broken links, complicated tutorials and puzzles that you need to put together to get it working.
So what is this?
Hopefully, an out of the box automatic test runner for both Actionscript3 and Actionscript2 (I couldn’t find any other for As2). Included are also some small unit tests so you can start playing with it fast (out of the box, right?).
By automatic I understand a test runner that compiles, runs and closes without the need of a user, but only through a script, and prints all it’s output to stdout. All of these are necessary conditions for setting up a continuous build, something that is very desirable (since we are all very lazy).
If you are really lazy
If you are too lazy (or too good ) to read all this, just download as_test_runner.zip and try it out. If it doesn’t work, come back and read on. I suspect you’ll come back.
I’ve worked on this on my MacOS X, I also tested on Linux. It should work on Windows too (but I admit I have not tested yet, I should do it these days).
On your machine, you also need:
a standalone Flash Player 9 in your command path (I’ve put mine under /usr/local/bin on linux). You can download it from here.
on MacOSX you have to make sure that the standalone Flash Player is the default app for opening swf files. If from a terminal you type “open SomeExampleSwf.swf” and the flash player opens with that file, it’s great. If not, make that happen.
you need javac, ant, mtasc (if you want As2) and the flex sdk (if you want As3). If you are thinking of Unit Testing, you should be able to install those yourself.
Steps to run your first unit tests
1. Make sure the system prerequisits are met
1.1. If you plan on running the As2 Unit Tests you need to do an extra step, you need to add your path to the FlashPlayerTrust directory.
On MacOS: Create a directory /Library/Application Support/Macromedia/FlashPlayerTrust
On Linux: Create a directory /etc/adobe/FlashPlayerTrust (or ~/.macromedia/Flash_Player/#Security/FlashPlayerTrust)
On Windows: C:\windows\system32\Macromedia\Flash\FlashPlayerTrust
In that directory, create a “trust.cfg” file and add a path to your favorite directory in that file. This will tell the flash player to trust the swf files in that directory.
2. Download as_test_runner.zip and extract it in your favorite directory.
3. Edit build.properties such that the mtasc and flex.sdk paths match your configuration.
4. From the directory where you extracted the archive, in a terminal, type “ant”.
You should be all done. If it doesn’t work, let me know (I swear, it works for me ).
[update] You should also take a look at the video here, a project that became ProjectSprouts (don’t let the name turn you off). Worth checking out.[/update]
What might not work
This should work on Linux, MacOS and Windows. I only tested on MacOS (where I cleaned everything up) and Linux.
If you have trouble with the security sandbox (the swf opens, but it throws an error) read more about FlashPlayerTrust.
If ‘ant’ has trouble launching the flash player, there is some issue with your standalone flash player and path.
If you have trouble compiling your swf’s, there are issues with MTASC and the Flex compiler. Check that they are set up correctly and that you can manually compile stuff with them.
Some final thoughts
This is put together starting from the work done here by a guy named Peter Martin. As all the other tutorials they are made from many pieces and you have to tune everything for it to work, so I wanted a super easy out of the box solution. Credits do go to Peter Martin, I just cleaned his work, made it output to stdout and made it work with As2.
A good resource for related work done by various people is here.
In order to make AsUnit (actionscript2) work, I had to make some very very slight changes to AsUnit (to allow a listener deeper investigation of
the framework). This means you can’t just replace AsUnit code with newer code, it will stop working. The good news is, I got it working for As2 (which nobody else did as far as I can tell, they are all blinded by flexunit ).
FlexUnit is out of the box (no changes there).
I’ve left the Eclipse/Flex files in the directory, so ideally you could import this as a project into Eclipse and simply run it with Ant, so that you could make development and testing a breeze.
The runner produces some XML output too, for some software called Cruise Control (I never used it). I had to change the java socket listener to display text output (this was a substantial part of the work, along with the as2 port).