Using ASUnit with ActionScript 2.0

ASUnit is a framework that allows ActionScript developers to create, build, and run test suites. This post demonstrates how to use the ASUnit Extension with AS2.0 and avoids the topic of why a programmer should use test driven development.

Installation

  1. Close any Macromedia or Adobe applications
  2. Download and install the appropriate Extension Manager for your version of Flash and OS from:
    http://www.adobe.com/exchange/em_download/
  3. Download and install the ASUnit Extension from:
    http://sourceforge.net/project/showfiles.php?group_id=108947&package_id=208529
  4. Open Flash

Verifying the Installation

In Flash, go to the Commands menu and verify the “Create Class” command is available. If the “Create Class” command is not present verify the ASUnit extension is installed and enabled in the Extension Manager. You shouldn’t need to restart your machine.

Getting Started

  1. Create a new AS2.0 FLA in an empty directory.
  2. Save the FLA to an empty directory. ASUnit will need to create files relative to the FLAs path.
  3. Insert the following code on Layer 1, Frame 1
    import AllTests;
    var at:AllTests = new AllTests();
  4. Go to the Commands menu, Create Class menu item
    Create Class
  5. Enter a qualified Class Name
  6. Uncheck “Add to Library as MovieClip.”
  7. Click OK
  8. Go to the Commands menu and select the Build Test Suites command:
    Built Tests

Potential Problems

  • If you don’t enter a namespace when creating a new class ASUnit will output the following error:
    Create Class Error

    This is easy to fix; just enter “com.Example” in the Class Name text box (highlighted in blue above). All classes must use namespaces.

  • If testing the movie does not present you with test case results, go to the Window Menu, Other Panels sub-menu, AsUnit Ui item to show the ASUnit panel.

Recommended Procedures

  1. Setup your project so unit testing is performed outside the development area. I usually setup a directory called “unit_testing” and store my tests there. All development is done in the “development.” folder. The testing FLA Publish Settings must include the “development” folder in its search path list; this is usually entered as a relative URL.
    Folder structure
  2. The “Create Class” command creates two files, a className.as and a classNameTest.as. Delete the className.as

I Want To Create Tests For Existing Code

If you’ve decided to add unit testing to your arsenal mid-project try the following:

  1. Add a directory in your project to hold the unit tests
  2. Create a new AS2.0 FLA in the new directory
  3. Go to the File menu, Publish Settings item, Settings… button
  4. Add your project’s class path to the Classpath list and close
  5. Add the code snippet above

Troubleshooting

If you run into class conflicts check to see if Flash’s global class paths are conflicting with the local Flash paths. To do this in CS3, go to the Edit menu, Preferences item, ActionScript category, ActionScript 2.0 Settings button. In reference to the example above, if your global class paths contain “./classes/” and your testing FLA class paths contain “../flash/classes/” Flash will happily notify you of a conflict.

Resources

Leave a Reply

This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)

You must read and type the 5 chars within 0..9 and A..F, and submit the form.

  

Oh no, I cannot read this. Please, generate a