Very recently i got a chance to work with AsUnit. We were looking for a
testing framework for flash and after googling, we found this tool called
AsUnit. It's an open source unit test framework for flash. Because of no documentation it was very hard to make it working for my first testing application. Most of the links given in this page point to does not exist pages on the web. After some more googling on this gave me some idea of how this works. Please find the reference of these websites from where i got some help to move forward with
AsUnit at the end of this post.In this post i will explain a very simple application using AsUnit.
AsUnit provides three ways to use their testing application in your application.
1. Framework
2. XUL UI
3. MXP
In the below example we will use the first one i.e. Framework. To start with below example, you have to first download the Framework from
here. It's a zip file. You can unzip this file in to your hard disk location. This will generate some folders named as2, as3, as3docs and as25. As clear from name, these folders are for different Actionscript versions. Since the below example is in Actionscript 3.0, we will use as3 folder.
To start with the example, First create a folder say TestExample in any location of your hard disk. Copy this as3 folder to TestExample folder. Now it's time to create the sample application which we will test with AsUnit.
Create a file named Example.as in the TestExample folder like this.
package {
public class Example {
public function add(num1:Number,num2:Number):Number{
return num1 + num2;
}
}
}
Now its time to create test case for this class. Create a file named ExampleTest in the same folder like this.
package {
//The below line import the AsUnit library of TestCase
import asunit.framework.TestCase;
public class ExampleTest extends TestCase {
//reference of Example class created above
private var example:Example;
public function ExampleTest(testMethod:String) {
super(testMethod);
}
//Default method for object instantiation
protected override function setUp():void {
example = new Example();
}
//Default method for Object deletion
protected override function tearDown():void {
example = null;
}
public function testInstantiated():void {
assertTrue("Example instantiated", example is Example);
}
public function testFail():void {
assertFalse("failing test", true);
}
/**
* Test the add method of Example class created above
*/
public function testAdd():void {
var result:Number = example.add(3,3);
assertEquals("Expected:6 Received:"+result, result, 6);
}
}
}
Now our test case is ready, Another thing we have to do is to create the TestSuite for executing many test cases together. Create a file named ExecuteAllTests.as in the same folder like this.
package {
import asunit.framework.TestSuite;
import ExampleTest;
public class ExecuteAllTests extends TestSuite {
public function ExecuteAllTests () {
super();
//Adding first test
addTest(new ExampleTest("testInstantiated"));
//Adding second test
addTest(new ExampleTest("testAdd"));
}
}
}
Till here we are ready with our main application, test case, test suite, Now we will create a runner class to run this test suite to produce some results.
Create a file named ExampleTestRunner.as in the same folder like this.
package {
import asunit.textui.TestRunner;
public class ExampleTestRunner extends TestRunner {
public function ExampleTestRunner() {
start(ExecuteAllTests, null, TestRunner.SHOW_TRACE);
}
}
}
As from the above structure you can see this runner class is taking our TestSuite class as a parameter and the last parameter is a boolean to show and hide the trace.
Now it's time to run the application. For this create a flash file named ExampleTestRunner.fla in the same folder. This file will be empty file. Just you need to put ExampleTestRunner in Document class under properties tab. This step is to avoid the below error while compiling the fla file.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at asunit.textui::TestRunner/asunit.textui:TestRunner::addedHandler()
at flash.display::DisplayObjectContainer/addChild()
at asunit.textui::TestRunner/setPrinter()
at asunit.textui::TestRunner/doRun()
at asunit.textui::TestRunner/start()
at ExampleTestRunner$iinit()
at ExampleTestRunner_fla::MainTimeline/ExampleTestRunner_fla::frame1()
The more detail on this can be found from the references given below. The below image will show how to use Document class in Flash CS3.

Its time to run the flash file and see the result. Test the flash movie by hitting Ctrl+Enter. YOu will see a screen like this if everything goes fine.
The above screen shows that both the tests are passed, Now go to ExampleTest.as file again and change this line assertEquals("Expected:6 Received:"+result, result, 6);
to assertEquals("Expected:6 Received:"+result, result, 7); and again run the application, you will see a screen like this saying one test fail in two tests.

References:-
1.
AsUnit testing with Actionscript 3.0 and flash CS 3.0 2.
AsUnit Step by Step3.
Migrating from flash 8 to flash CS34.
AsUnit Step by Step part 1 pdf5.
AsUnit Step by Step part 2 pdf