[转]Flex unit test&&flex-4-create-a-simple-unit-test-in-flash-builder

原文:http://www.flashas.net/flex/200906094229.html

原文:http://www.hufkens.net/2009/06/flex-4-create-a-simple-unit-test-in-flash-builder/

Flash Builder(Flex Builder) 4 Beta集成了flex unit test框架,在flash builder中进行单元测试更加方便了。Flash Builder可以帮助开发人员生成TestCase和TestSuite相关的类框架,开发人员只需要对具体的测试逻辑进行编码即可。在run菜单里可 以直接运行测试,flash builder会自动寻找相关的测试类,并执行他们。下面图解创建单元测试的过程。

 

1.创建测试用例(TestCase)

 创建测试用例

2.选择待测试的类

 选择待测试类

3.选择要测试的方法

 选择要测试的方法

4.编写测试逻辑

 

5.运行测试,回收测试结果

 选择测试测试结果

Flash Builder 4 beta中集成flex unit test框架后,让这一框架的集成性得到提高,方便了单元测试开发。

P.S.在新版的builder中,查找(Find in files)的速度得到了极大地提升。很不错。


One of the new features of Flash Builder is the support for FlexUnit (the Unit testing framework for Flex applications). You could use FlexUnit in the past, but as from Flash Builder it’s a piece of cake to create Test classes for your own code. You just need to follow these simple steps.

NOTE: This demo needs the latest beta version of Flash Builder. Be aware that this is still a beta version and the following could still change in future versions of Flash Builder.

Before we start we need to create a new Flex Project. After creating your project you need to create a class with some functionality that we can use in this example. The typical classes to unit test are classes that have a public interface and have a complicated implementation. When you change something inside the class implementation, the public methods still works as expected. But for the sake of simplicity we will create a very simple class with some simple behaviour.

package net. hufkens 
{
   /**
   * Class that does something.
   * 
   * @author alain
   * 
   */
 
   public  class MyObject
   {
     /**
     * The constructor 
     */
   
     public  function MyObject ( )
     {
     }
    
     /**
     * Returns the numeric vallue five
     * @return 5
     */
   
     public  function giveMeFive ( ): int
     {
       return  5;
     }
    
     /**
     * Returns the letter A
     * @return "A"
     */
   
     public  function sayA ( ): String
     {
       return  "B";
     }
    
     /**
     * Returns always true
     * @return true
     */
   
     public  function alwaysTrue ( ): Boolean
     {
       return  true;
     }
   }
}

Alright, now we have a Class that we can test. To create a new Unit Test you select New >TestCase Class and the following window should appear:

New Test Case Class

Enter the Name of the TestCase Class (in this case: MyTestClass), select the Class to test (in this case: MyObject) and select Finish. Flash Builder now generates several items. First a new folder name flexUnitTests is generated. Also a new mxml file namedflexUnitCompilerApplication.mxml is generated. This file is necessary because the Unit Tests need a swf application that can be started and the tests can be executed. The last and most important file that’s been generated is the TestCase itself. The only thing you now need to do is implement the actual test methods. In the following code example the test methods are implemented:

package flexUnitTests
{
   import flexunit. framework. Assert;
   import flexunit. framework. TestCase;
  
   import net. hufkens. MyObject;

   public  class MyTestClass  extends TestCase
   {
     // Reference declaration for class to test
     private  var classToTestRef : net. hufkens. MyObject;
    
     public  function MyTestClass (methodName: String= null )
     {
       super (methodName );
     }
    
     //This method will be called before every test function
    override  public  function setUp ( ): void
     {
       super. setUp ( );
      classToTestRef =  new MyObject ( );
     }
    
     //This method will be called after every test function
    override  public  function tearDown ( ): void
     {
       super. tearDown ( );
     }
    
     public  function testGiveMeFive ( ): void
     {
      Assert. assertEquals (classToTestRef. giveMeFive ( ), 5 );
     }
    
     public  function testSayA ( ): void
     {
      Assert. assertEquals (classToTestRef. sayA ( )"A" );
     }
    
     public  function testIsTrue ( ): void
     {
      Assert. assertTrue (classToTestRef. alwaysTrue ( ) );
     }

   }
}

If we now right-click the project and select Execute FlexUnit Tests we get the following message:

FlexUnit warning message

It says we need to create a Test Suite, otherwise Flash Builder can’t execute the tests. To create a new Test Suite you select New > TestSuite Class and the following window should appear:

New TestSuite Class

Now select Execute FlexUnit Tests again and your Test are executed. As you can see, one of the tests has failed. A quick view in the source of sayA() method reveals the problem. Fix the problem and all the tests should succeed now.

Unit Test Results

That’s all there is to it. Flash Builder makes it a lot easier to create Unit Tests for your libraries and components. This is a good thing, so you should definitely check it out.

转载于:https://www.cnblogs.com/sun11086/archive/2009/10/15/1583609.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值