使用ASUnit单元测试框架测试ActionScript代码

您所在的位置:网站首页 Actionscript20 使用ASUnit单元测试框架测试ActionScript代码

使用ASUnit单元测试框架测试ActionScript代码

#使用ASUnit单元测试框架测试ActionScript代码| 来源: 网络整理| 查看: 265

使用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 3. public class AsUnitTestRunner extends 4. public 5. 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. package{ 2. public class 3. public 4. return 5. } 6. } 7. }

5)为例子类创建一个测试

在Flash IDE,从菜单File->New->ActionScript File,新建一AS文件,另存文件为AsUnitExample/ExampleTest.as。粘贴下列代码:

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

6)创建测试集

在Flash IDE,从菜单File->New->ActionScript File,新建一AS文件,另存文件为AsUnitExample/AllTests.as。粘贴下列代码:

1. package{ 2. import 3. public class AllTests extends 4. public 5. super(); 6. new ExampleTest("testInstantiated")); 7. new ExampleTest("testAddition")); 8. } 9. } 10. }

7)在Flash IDE,编译AsUnitTestRunner.fla(通过运行Control->Test Movie)。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3