How to Use the ASUnit Framework to Write Unit Tests for ActionScript

folder-explorer AsUnit is the first choice for Test-Driven Development using pure Flash applications.It is an Open-Source, Unit Test Framework for Macromedia Flash ActionScript 2.0 and 3.0. AsUnit 2.x is fully integrated with the Flash IDE, and there is a Mozilla XUL UI that works alongside any other ActionScript authoring tools.  This framework allows developers to easily create and manage Classes, Test Cases, Test Suites and view the results of extensive test fixtures. This article will explain in detail how to use the ASUnit framework to write unit tests for ActionScript. It is aimed at an experienced AS developer who is curious about unit testing but has been frustrated by the lack of documentation for ASUnit.

1) Create a directory called AsUnitExample
Download the asunit framework zip file from here and copy the contents of the as3 directory into your AsUnitExample directory. All of the files we create in the following example will be in the root directory AsUnitExample. At this point the AsUnitExample directory should contain the file AsUnitTestRunner.as and the directories asunit and mx.

2) Open and inspect the file AsUnitExample/AsUnitTestRunner.as
Copy the following source code:

package {
	import asunit.textui.TestRunner;

	public class AsUnitTestRunner extends TestRunner {
		public function AsUnitTestRunner() {
			start(AllTests, null, TestRunner.SHOW_TRACE);
		}
	}
}

3) Create an .fla file to serve as the test container

In the Flash IDE, File->New->Flash File (Actionscript 3). Save the file as AsUnitExample/AsUnitTestRunner.fla. In the properties window, set the Document class in the properties to be AsUnitTestRunner. Save the file again.

4) Create a sample class for us to run tests on

Paste in the following souce code into AsUnitExample/Example.as:

package {

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

5) Create a test for our sample class

In the Flash IDE, File->New->ActionScript File. Save the file as AsUnitExample/ExampleTest.as. Paste in the following:

package {
  import asunit.framework.TestCase;

  public class ExampleTest extends TestCase {
    private var _instance:Example;

    /**
      * Constructor
      *
      * @param testMethod Name of the method to test
      */
     public function ExampleTest(testMethod:String) {
       super(testMethod);
     }

    /**
      * Prepare for test, create instance of class that we are testing.
      * Invoked by TestCase.runMethod function.
      */
    protected override function setUp():void {
       _instance = new Example();
     }

    /**
      * Clean up after test, delete instance of class that we were testing.
      */
     protected override function tearDown():void {
       _instance = null;
     }

    /**
      * Test of whether or not class properly instantiated
      */
     public function testInstantiated():void {
       assertTrue("Example instantiated", _instance is Example);
     }

    /**
      * Test that is born to lose.
      */
     public function testFail():void {
       assertFalse("failing test", true);
     }

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

6) Create the test suite

In the Flash IDE, File->New->ActionScript File. Save the file as AsUnitExample/AllTests.as. Paste in the following:

package {
  import asunit.framework.TestSuite;

  public class AllTests extends TestSuite {
    public function AllTests() {
       super();
      addTest(new ExampleTest("testInstantiated"));
       addTest(new ExampleTest("testAddition"));
       //addTest(new ExampleTest("testFail"));
     }
  }
}

7) In the Flash IDE, compile AsUnitTestRunner.fla by running Control->Test Movie

Cheers!

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

3 Responses to “How to Use the ASUnit Framework to Write Unit Tests for ActionScript”

  1. [...] > How to Use the ASUnit Framework to Write Unit Tests for ActionScript – Ntt.cc [...]

  2. Pavan says:

    This is a good tutorial!

    Can you please tell me any tools for flash load testing?

Leave a Reply