天天看點

使用ASUnit單元測試架構測試ActionScript代碼

版權聲明:本文為部落客chszs的原創文章,未經部落客允許不得轉載。 https://blog.csdn.net/chszs/article/details/3008148 使用ASUnit單元測試架構測試ActionScript代碼

ASUnit是使用純Flash應用程式進行測試驅動開發的首選工具。它是開源的單元測試架構,可用于ActionScript2.0、3.0。

ASUnit2.x完全和Flash IDE內建,支援Mozilla的XUL UI。此架構運作開發者輕松建立和管理類、測試用例、測試集,并浏覽特定的測試結果。

本文解釋了怎樣使用ASUnit架構建立ActionScript單元測試。

1)建立名為AsUnitExample的目錄

下載下傳ASUnit架構的zip檔案,位址為:http://sourceforge.net/project/showfiles.php?group_id=108947&package_id=208528

複制ActionScript3目錄的内容到AsUnitExample目錄。本文以AsUnitExample目錄為項目根目錄。且AsUnitExample目錄應包含檔案AsUnitTestRunner.as和asunit目錄、mx目錄。

2)打開并檢查AsUnitExample/AsUnitTestRunner.as

複制下面的源代碼:

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

3)建立一個.fla檔案服務于測試容器

在Flash IDE,從菜單File->New->Flash File(ActionScript3),建立一個新的Flash檔案,儲存檔案為AsUnitExample/AsUnitTestRunner.fla。在屬性視窗,設定檔案類屬性為AsUnitTestRunner。儲存它。

4)建立一簡單的類,并運作測試

粘貼下列源碼到AsUnitExample/Example.as檔案。

  1.     public class Example{
  2.         public function add(num1:Number, num2:Number):Number{
  3.             return num1+num2;

5)為例子類建立一個測試

在Flash IDE,從菜單File->New->ActionScript File,建立一AS檔案,另存檔案為AsUnitExample/ExampleTest.as。粘貼下列代碼:

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

6)建立測試集

在Flash IDE,從菜單File->New->ActionScript File,建立一AS檔案,另存檔案為AsUnitExample/AllTests.as。粘貼下列代碼:

  1.     import asunit.framework.TestSuite;
  2.     public class AllTests extends TestSuite{
  3.         public function AllTests(){
  4.             super();
  5.             addTest(new ExampleTest("testInstantiated"));
  6.             addTest(new ExampleTest("testAddition"));

7)在Flash IDE,編譯AsUnitTestRunner.fla(通過運作Control->Test Movie)。

下一篇: Web開發反思