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

使用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. importasunit.textui.TestRunner;
  3. publicclassAsUnitTestRunnerextendsTestRunner{
  4. publicfunctionAsUnitTestRunner(){
  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. package{
  2. publicclassExample{
  3. publicfunctionadd(num1:Number,num2:Number):Number{
  4. returnnum1+num2;
  5. }
  6. }
  7. }

5)为例子类创建一个测试
在Flash IDE,从菜单File->New->ActionScript File,新建一AS文件,另存文件为AsUnitExample/ExampleTest.as。粘贴下列代码:
  1. package{
  2. importasunit.framework.TestCase;
  3. publicclassExampleTestextendsTestCase{
  4. privatevar_instance:Example;
  5. /**
  6. *Constructor
  7. *
  8. *@paramtestMethodNameofthemethodtotest
  9. */
  10. publicfunctionExampleTest(testMethod:String){
  11. super(testMethod);
  12. }
  13. /**
  14. *Preparefortest,createinstanceofclassthatwearetesting.
  15. *InvokedbyTestCase.runMethodfunction.
  16. */
  17. protectedoverridefunctionsetUp():void{
  18. _instance=newExample();
  19. }
  20. /**
  21. *Cleanupaftertest,deleteinstanceofclassthatweweretesting.
  22. */
  23. protectedoverridefunctiontearDown():void{
  24. _instance=null;
  25. }
  26. /**
  27. *Testofwhetherornotclassproperlyinstantiated
  28. */
  29. publicfunctiontestInstantiated():void{
  30. assertTrue("Exampleinstantiated",_instanceisExample);
  31. }
  32. /**
  33. *Testthatisborntolose.
  34. */
  35. publicfunctiontestFail():void{
  36. assertFalse("failingtest",true);
  37. }
  38. /**
  39. *Testtheadditionmethodonexample
  40. */
  41. publicfunctiontestAddition():void{
  42. varresult:Number=_instance.add(2,3);
  43. assertEquals("Expected:5Received:"+result,result,5);
  44. }
  45. }
  46. }

6)创建测试集
在Flash IDE,从菜单File->New->ActionScript File,新建一AS文件,另存文件为AsUnitExample/AllTests.as。粘贴下列代码:
  1. package{
  2. importasunit.framework.TestSuite;
  3. publicclassAllTestsextendsTestSuite{
  4. publicfunctionAllTests(){
  5. super();
  6. addTest(newExampleTest("testInstantiated"));
  7. addTest(newExampleTest("testAddition"));
  8. }
  9. }
  10. }

7)在Flash IDE,编译AsUnitTestRunner.fla(通过运行Control->Test Movie)。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值