2010年03月30日   测试之路   5,588 次浏览

       Flex单元测试与Junit有很大的不同,而Flexunit是用于flex的单元和集成测试的,编写测试代码之前首先到http://opensource.adobe.com/wiki/display/flexunit/Downloads去下载相应的flexunit包。

         在MyEclipse中右击工程,选择属性,选择”java build path”,选择第二个选项卡,将flexunit.swc加入到工程,单击“确定”,就可以编写相应的单元测试代码了。

        实例:新建要测试的文件:代码如下,这里使用的是ActionScript。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package test{
 public class BankAcount{
  private var balance:Number = 0 ;
  
  public function deposite(amount:Number):void{
   balance = balance + amount;
  }
 
  public function withdraw(amount:Number):void{
   balance = balance - amount;
  }
  
  public function getBalance():Number{
   return balance;
  }
 }
 
}

新建测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 package test{
 import flexunit.framework.TestCase;
 
 public class BankAcountTest extends TestCase{
  
  public function testDeposite():void{
   var bankAcount:BankAcount = new BankAcount();
   bankAcount.deposite(50);
   assertTrue("BankAcount on a new account is 50 after deposite 50",50,bankAcount.getBalance() == 50);
  }
  
  public function testWithDraw():void{
   var bankAcount:BankAcount = new BankAcount();
   bankAcount.deposite(100);
   bankAcount.withdraw(50);
   assertTrue("BankAcount on a new account is 50 after withdraw 50",10,bankAcount.getBalance() == 40);
   
  }
 }
}

新建一个mxml文件,用于运行测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="<a href="http://www.adobe.com/2006/mxml">http://www.adobe.com/2006/mxml</a>" layout="absolute"
 xmlns:flexunit="flexunit.flexui.*"
  creationComplete="onCreationComplete()">
 <mx:Script>
  <![CDATA[
      import flexunit.framework.TestSuite;
      import test.BankAcountTest;
   
    private function onCreationComplete():void{
     testRunner.test = createSuite();
     testRunner.startTest();
    }
   
    private function createSuite():TestSuite{
     var testSuite:TestSuite = new TestSuite();
     testSuite.addTestSuite(BankAcountTest);
     return testSuite;
    }
  ]]>
 </mx:Script>
 
 <flexunit:TestRunnerBase id="testRunner" width="100%" height="100%"/>
</mx:Application>

现在可以运行了,如果要测试的代码在mxml文件里也是可以运行的,更改testSuite.addTestSuite(BankAcountTest);括号里面的类名为mxml文件名就可以了。

赞 赏
申明:除非注明,本站文章均为原创,转载请以链接形式标明本文地址。 如有问题,请于一周内与本站联系,本站将在第一时间对相关内容进行处理。
本文地址: http://www.yyjjssnn.cn/articles/365.html
相关阅读: Flex, Flex单元测试

>>> Hello World <<<

这篇内容是否帮助到你了呢?

如果你有任何疑问或有建议留给其他朋友,都可以给我留言。

:wink: :twisted: :surprised: :smile: :smile9: :smile8: :smile7: :smile6: :smile5: :smile56: :smile55: :smile54: :smile53: :smile52: :smile51: :smile50: :smile4: :smile49: :smile48: :smile47: :smile46: :smile45: :smile44: :smile43: :smile42: :smile41: :smile40: :smile3: :smile39: :smile38: :smile37: :smile36: :smile35: :smile34: :smile33: :smile32: :smile31: :smile30: :smile2: :smile29: :smile28: :smile27: :smile26: :smile25: :smile24: :smile23: :smile22: :smile21: :smile20: :smile1: :smile19: :smile18: :smile17: :smile16: :smile15: :smile14: :smile13: :smile12: :smile11: :smile10: :smile0: :sad: :rolleyes1: :redface: :razz: :question: :neutral: :mrgreen: :mad: :lol: :idea: :exclaim: :evil: :eek: :cry: :cool: :confused: :biggrin: :arrow:

友情链接: 程序员刘杨 刘杨
Copyright 2003~2018 保留所有权利 | 网站地图
备案号:湘ICP备14001005号-2

湘公网安备 43011102001322号