代码改变世界

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

2010-04-22 12:49  宝宝合凤凰  阅读(197)  评论(0)    收藏  举报
 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:

  1. package {
  2.     import asunit.textui.TestRunner;
  3.  
  4.     public class AsUnitTestRunner extends TestRunner {
  5.         public function AsUnitTestRunner() {
  6.             start(AllTests, null, TestRunner.SHOW_TRACE);
  7.         }
  8.     }
  9. }

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:

  1. package {
  2.  
  3.     public class Example {
  4.         public function add(num1:Number,num2:Number):Number{
  5.              return num1 + num2;
  6.          }
  7.      }
  8. }

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:

  1. package {
  2.   import asunit.framework.TestCase;
  3.  
  4.   public class ExampleTest extends TestCase {
  5.     private var _instance:Example;
  6.  
  7.     /**
  8.       * Constructor
  9.       *
  10.       * @param testMethod Name of the method to test
  11.       */
  12.      public function ExampleTest(testMethod:String) {
  13.        super(testMethod);
  14.      }
  15.  
  16.     /**
  17.       * Prepare for test, create instance of class that we are testing.
  18.       * Invoked by TestCase.runMethod function.
  19.       */
  20.     protected override function setUp():void {
  21.        _instance = new Example();
  22.      }
  23.  
  24.     /**
  25.       * Clean up after test, delete instance of class that we were testing.
  26.       */
  27.      protected override function tearDown():void {
  28.        _instance = null;
  29.      }
  30.  
  31.     /**
  32.       * Test of whether or not class properly instantiated
  33.       */
  34.      public function testInstantiated():void {
  35.        assertTrue("Example instantiated", _instance is Example);
  36.      }
  37.  
  38.     /**
  39.       * Test that is born to lose.
  40.       */
  41.      public function testFail():void {
  42.        assertFalse("failing test", true);
  43.      }
  44.  
  45.     /**
  46.       * Test the addition method on example
  47.       */
  48.      public function testAddition():void {
  49.        var result:Number = _instance.add(2,3);
  50.        assertEquals("Expected:5 Received:"+result, result, 5);
  51.      }
  52.   }
  53. }

6) Create the test suite

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

  1. package {
  2.   import asunit.framework.TestSuite;
  3.  
  4.   public class AllTests extends TestSuite {
  5.     public function AllTests() {
  6.        super();
  7.       addTest(new ExampleTest("testInstantiated"));
  8.        addTest(new ExampleTest("testAddition"));
  9.        //addTest(new ExampleTest("testFail"));
  10.      }
  11.   }
  12. }

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

Cheers!