Click here to get a complete list of blogentries.

Getting Started With AsUnit 3.0 and FlashDevelop3 in AS3 (Unit Tests)

This is a guide to get AsUnit to work with FlashDevelop. I assume that you have a working installation of FlashDevelop up and running. If not you may use this guide to get there.

For this guide I used FlashDevelop 3.0.0 Beta7 and AsUnit 3.0 2007-10-11 version (release 162).

1. Download AsUnit

Go to and download the "Framework". It will contain all the classes and sources you will need to get your unit-testing framework going.

2. Setting up a project

Start FlashDevelop and select "New Project" in the "Project" menu. Create a new AS3 project, specify a directory to create the project in and name your project. I called it MapMeister, don't ask why. ;)

In the project's directory create a folder for the sources (e.g. "src") and one for the binaries (e.g. "bin"). The two directories should appear in the "Project Manager"-Control. If you cannot see the the control, you can switch it on by activating the "Project Manager" checkbox from the "View" menu.

Now, right-click on the source folder ("src") and choose "Add" and select "New Class" from the pop up. Enter "" in the input field and hit Ok. The class stub of the main class will be added to the sources.

Starting now will cause a build failure with a

FlashDevelop: "This project does not have a main class" error.

To fix this right-click on the "" (the as-file that contains the Main class) in the "Project Manager" and turn "Always Compile" on.

The main class needs to extend flash.display.Sprite, so it should look like this:

Listing 1 :

Afterwards, open the "Properties" tab from the "Project" menu. Open the output tab and enter the name of the binary to be created including the binary folder, e.g. "binAsUnitTest.swf".

Setting output file in FlashDevelop 3
Image 1 - Setting output file in FlashDevelop 3

3. Adding AsUnit to your project

I assume you have already downloaded the AsUnit-framework archive. Extract it into a convenient directory. Afterwards, add the framework's AS3 sources to the classpath of your project. To do so open the "Properties" window again from the "Projects" menu. Select the "Classpaths" tab and hit the "Add Classpath" button. Add the "as3" directory of the framework.

Setting classpath in FlashDevelop 3 for AsUnit
Image 2 - Setting classpath in FlashDevelop 3 for AsUnit

Well, I personally recommend to create a new directory for the test cases. Right-click on the project in the "Project Explorer" and select "Add" and then "New Folder". Call it e.g. "testsrc".

FlashDevelop's Project Manager shows the project structure
Image 3 - FlashDevelop's Project Manager shows the project structure

Now, it's time to write the first class, test suite. Right-click on the test-source folder and add a new class called "". This class needs to extend asunit.framework.TestSuite, so it should like that:

Listing 2 :

This class will later contain references to all our tests. This class needs to be passed to the AsUnit framework within the Main-class' constructor like this (Please be aware that in this listing we are passing the stage object to the TestRunner class. This is not part of the original implementation. This is a workaround for a runtime error that occurs with AsUnit and FlashDevelop [check problems section at the end of this tutorial].):

Listing 3 :, starting AsUnit

If you start the project now, you will see the gray AsUnit screen. This means you are ready to go!

4. Writing a test

Add another class to the test-source folder. e.g. "". This class needs to extend asunit.framework.TestCase. Well, for every class you want to test, create a test class here. Add methods to the test class with each method covering a certain aspect of the functionality.

Your first test may look like that:

Listing 4 :

For the test to be executed, you need to add a reference to the AllTests-class like this:

Listing 5 :, adding test references

Now you may run the project. If everything is ok, you should see an AsUnit error message stating that one test failed.

AsUnit Failure message
Image 4 - AsUnit Failure message

Change the "8" in the TestFloatMath test into a "9" and the failure disappears.

You're ready to go. Get them, tiger!

5. Problems

5.1 TypeError: Error #1009: Cannot access a property or method of a null object reference. (

The reason for this problem is that the TestRunner is not added to the stage. If for some reason you do not want to add the TestRunner to the stage, here is an alternative to solve the problem:

(1) Open the file asunitas3asunit
(2) Locate the constructor and add a parameter to pass the stage to the TestRunner like this:

Listing 6 :, Adding Stage parameter

(3) Replace all occurences of "stage" within the file with "m_parent", which should give you something like this (only the altered methods are shown):

Listing 7 :, Replacing direct access to stage with member variable

This should fix that problem. :) But it's easier to just add the TestRunner to the stage in (Thanks to John for pointing that out.)

Posted on 2009/1/7
blog comments powered by Disqus
homecoded facebook logo
This is a picture of my address. I am not going to put that in plain writing!
Small print: