Sunday, August 8, 2010

Unit Testing For ActionScript 3.0

I was curious to see whether anyone had written unit test software for ActionScript 3.0. To my pleasant surprise I found AsUnit. With this software I found a tutorial. I followed the instructions and soon realised there had been some changes since the tutorial was posted in July 2007. Here is how I was able to get AsUnit (installed from AsUnit-20070108.msi) running with Adobe Flash CS3 Professional. 

1) Download the software from SourceForge. 

2) Install the software. I chose to install the software to this folder:

E:\Projects\Flash\asunit

3) Add the classpath to the Flash IDE preferences. From the main menu Edit -> Preferences. Select category ActionScript. Click ActionScript 3.0 Settings button. Add a new classpath for the framework\as3 folder of the install directory

E:\Projects\Flash\asunit\framework\as3

Click OK to close the ActionScript 3.0 Settings window. Click OK to close the Preferences window. The AsUnit code can now be a part of all future projects

Following is an example showing how the unit test code works

1) Create a new folder for the example. I created my folder here:

E:\Projects\Flash\tutorial

2) Create a file called AsUnitTestRunner.as with the following code:

package {
    import asunit.textui.TestRunner;

    public class AsUnitTestRunner extends TestRunner {
        public function AsUnitTestRunner() {
            start(AllTests, null, TestRunner.SHOW_TRACE);
        }
    }
 
I am led to believe this file was included in earlier versions of the ASUnit code. It was not present in the files installed by AsUnit-20070108.msi.

3) Run AsUnit.exe. This is a wizard to help set up some of the unit test code.

E:\Projects\Flash\asunit\AsUnit.exe 

The first time this runs, the developer is prompted for information by various windows. The next time it runs the developer can enter information directly into text fields. This is the information required to continue the tutorial.

Projects: "Default Project 1". I did not change the default project name.

Source Folder: the folder created in Step 1).

Test Folder: the check-box for "Same as Source Folder" is ticked. This is satisfactory for the purposes of this tutorial. It may be better to keep the test code separate from the the source code in a larger project.

Templates: ActionScript 3.0 (Flash Player 9.0). It is likely this AsUnit code has not been modified for subsequent versions of Flash Player. Caveat emptor.

Class Path: this is the path to the classes required by Flash Player.

C:\Documents and Settings\\Local Settings\Application Data\Adobe\Flash CS3\en\Configuration\Classes 

Class Name: this is the name to give to the class we want to unit test. For this tutorial use the class Example. Now click the Create button. This will create the following three files in the tutorial folder

AllTests.as
Example.as
ExampleTest.as


a) Example.as is a skeleton class.

package  {
    public class Example {
        public function Example() {
        }
    }
}

b) ExampleText.as

This class extends the TestCase class. It contains the code for the setUp() and tearDown() functions that run respectively before and after every test. It also contains the function to test instantiation - testInstantiated(). And it contains a failing test. This last function is to encourage the developer to continue writing tests before adding functionality to the Example class.

c) AllTests.as

This class extends the TestSuite class. This class can run tests for as many classes as necessary. There is an import statement for each TestCase. In this instance there is only the ExampeTest class.

import ExampleTest

In the constructor for this class there is a line to run each test suite:

addTest(new ExampleTest());

4) There is now enough code to start running unit tests. Create a SWF to run the unit test. Open the Adobe Flash CS3 IDE and create a new ActionScript 3.0 FLA. Save the file as "tutorial.fla" in the tutorial folder. Set the document class to AsUnitTestRunner and save the file again.

5) Test the project from within the IDE.

AsUnit 3.0 by Luke Bayes and Ali Mills

..F

Time: 0.072
There was 1 failure:
0) ExampleTest.test()
AssertionFailedError: failing test
    at asunit.framework::Assert$/fail()
    at asunit.framework::Assert$/assertTrue()
    at ExampleTest/test()
    at asunit.framework::TestCase/::runMethod()
    at asunit.framework::TestCase/runBare()
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at ()
    at flash.utils::SetIntervalTimer/flash.utils:SetIntervalTimer::onTimer()
    at flash.utils::Timer/flash.utils:Timer::_timerDispatch()
    at flash.utils::Timer/flash.utils:Timer::tick()

FAILURES!!!
Tests run: 2,  Failures: 1,  Errors: 0

The unit test has run to completion. It has successfully picked up the fail test which was purposely designed to fail.

6) Add a function to the ExampleTest class to unit test an add() function

/**
 * Test the addition method on example
 */
public function testAddition():void {
    var result:Number = instance.add(2,3);
    assertEquals("Expected:5 Received:"+result, result, 5);
}

In the fashion of Agile programming we have added the unit test BEFORE the code. This is a good time to remove the fail test that was generated by the wizard.

But now the FLA will not compile. The compiler has detected a call to a possibly undefined method add through a reference with static type Example.

7) Add the function to the Example class to make the test compile.

public function add(num1:Number,num2:Number):Number{
    return num1 + num2;
}

8) Now we are ready to run the test again. This time the test compiles and runs without error.


I hope this helps other developers install AsUnit correctly. Please contact me if there are any issues as a result of following the instructions in this blog.


Thank-you for reading

No comments:

Post a Comment