小试FlexUnit(Flex单元测试)
最近学习flex,本人是从C/S架构过来的,用起来有很多的熟悉感,为了公司后续的开发工作顺利开展,了解了flex单元测试,感觉还是很不错的,费话不说了,过程如下:
1.下载FlexUnit0.9
http://opensource.adobe.com/wiki/display/flexunit/Downloads
2.解压后有两个目录,asDoc是API文档,Bin是SWC库
3.新建项目,如下图设定,按“Finish”新建项目
4.右键项目TestFlexUnit->Properties->FlexBuildPath,点击“AddSwc…”,加入flexUnit的swc包。
5.新建一个需要测试的业务逻辑类(ActionScript类),例下:
package { public class Arithmetic { /** * 加法 * author:LiuYX */ public static function add(a:Number,b:Number):Number{ return a + b; } /** * 减法 * author:LiuYX */ public static function subtract(a:Number,b:Number):Number{ return a - b; } } }
package { public class Arithmetic { /** * 加法 * author:LiuYX */ public static function add(a:Number,b:Number):Number{ return a + b; } /** * 减法 * author:LiuYX */ public static function subtract(a:Number,b:Number):Number{ return a - b; } } }
6.继承FlexUnit的TestCase类,新建测试业务的ArithmeticTest类,如下:
package { import flexunit.framework.TestCase; import flexunit.framework.TestSuite; public class ArithmeticTest extends TestCase { /** * 重写TestCase的构造器 * author:LiuYX * date:2009-01-07 */ public function ArithmeticTest(methodName:String) { //回调TestCase的构造器 super(methodName); } /** * 静态方法,返回这个TestCase包含的TestSuite * author:LiuYX * date:2009-01-07 */ public static function Suite():TestSuite{ var suite:TestSuite = new TestSuite(); suite.addTest(new ArithmeticTest("testAdd")); suite.addTest(new ArithmeticTest("testSubtract")); return suite; } /** * TestCast的测试方法,测试业务类的加法逻辑 * author:LiuYX * date:2009-01-07 */ public function testAdd():void{ var result:Number = 0; result = Arithmetic.add(1,2); assertTrue("和为 3",result == 3); result = Arithmetic.add(2,2); assertEquals("和为 4",4,result); } /** * TestCast的测试方法,测试业务类的减法逻辑 * author:LiuYX * date:2009-01-07 */ public function testSubtract():void{ var result:Number = 0; result = Arithmetic.subtract(2,1); assertTrue("结果为 1",result == 1); assertTrue("结果为 0",0,Arithmetic.subtract(2,2)); } } }
package { import flexunit.framework.TestCase; import flexunit.framework.TestSuite; public class ArithmeticTest extends TestCase { /** * 重写TestCase的构造器 * author:LiuYX * date:2009-01-07 */ public function ArithmeticTest(methodName:String) { //回调TestCase的构造器 super(methodName); } /** * 静态方法,返回这个TestCase包含的TestSuite * author:LiuYX * date:2009-01-07 */ public static function Suite():TestSuite{ var suite:TestSuite = new TestSuite(); suite.addTest(new ArithmeticTest("testAdd")); suite.addTest(new ArithmeticTest("testSubtract")); return suite; } /** * TestCast的测试方法,测试业务类的加法逻辑 * author:LiuYX * date:2009-01-07 */ public function testAdd():void{ var result:Number = 0; result = Arithmetic.add(1,2); assertTrue("和为 3",result == 3); result = Arithmetic.add(2,2); assertEquals("和为 4",4,result); } /** * TestCast的测试方法,测试业务类的减法逻辑 * author:LiuYX * date:2009-01-07 */ public function testSubtract():void{ var result:Number = 0; result = Arithmetic.subtract(2,1); assertTrue("结果为 1",result == 1); assertTrue("结果为 0",0,Arithmetic.subtract(2,2)); } } }
7.修改TestFlexUnit.mxml,加入flexunit的TestRunnerBase。
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:flexunit="flexunit.flexui.*" layout="absolute" creationComplete="onCreationComplete()"> <mx:Script> <![CDATA[ import flexunit.framework.TestSuite; /** * 创建整个项目的TestSuite * author:LiuYX */ private function createSuite():TestSuite { /*组建TestSuite*/ var suite:TestSuite = new TestSuite(); suite.addTest(ArithmeticTest.Suite()); /*TODO:在这加入其它TestSuite*/ return suite; } /** * 调用TestRunner进行测试 */ private function onCreationComplete():void { /*代入TestRunnerBase进行测试*/ testRunner.test = this.createSuite(); testRunner.startTest(); } ]]> </mx:Script> <!-- flexunit的TestRunnerBase界面组件 --> <flexunit:TestRunnerBase id="testRunner" width="100%" height="100%" /> </mx:Application>
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:flexunit="flexunit.flexui.*" layout="absolute" creationComplete="onCreationComplete()"> <mx:Script> <![CDATA[ import flexunit.framework.TestSuite; /** * 创建整个项目的TestSuite * author:LiuYX */ private function createSuite():TestSuite { /*组建TestSuite*/ var suite:TestSuite = new TestSuite(); suite.addTest(ArithmeticTest.Suite()); /*TODO:在这加入其它TestSuite*/ return suite; } /** * 调用TestRunner进行测试 */ private function onCreationComplete():void { /*代入TestRunnerBase进行测试*/ testRunner.test = this.createSuite(); testRunner.startTest(); } ]]> </mx:Script> <!-- flexunit的TestRunnerBase界面组件 --> <flexunit:TestRunnerBase id="testRunner" width="100%" height="100%" /> </mx:Application>
运行结果: