问题:最近项目需要用Flex搭建前端的界面,可是以前学java时有单元测试,于是乎查了查资料,找到相关资料,记录自己搭建测试过程,供日后参考。
解决方案:
1、建立flex工程
2、建立需要测试的逻辑类
3、建立Flex Test Suite Class 和Flex Test Case Class
4、写自己的测试逻辑,进行测试
测试代码如下:
CalculatorLogicHelper.as
package com { public class CalculatorLogicHelper { public function CalculatorLogicHelper() { } public static function add(num1:Number,num2:Number):Number { var num:Number = num1 + num2; return num; } } }
CalculatorTestSuite.as
package com.test { [Suite] [RunWith("org.flexunit.runners.Suite")] public class CalculatorTestSuite { public var calculatorLogic:CalculatorLogicTester; } }
CalculatorLogicTester.as
package com.test { import com.CalculatorLogicHelper; import flexunit.framework.Assert; public class CalculatorLogicTester { private var classToTestRef:CalculatorLogicHelper; public function CalculatorLogicTester(){} [Before] public function setUp():void { } [After] public function tearDown():void { } [BeforeClass] public static function setUpBeforeClass():void { } [AfterClass] public static function tearDownAfterClass():void { } [Test] public function testAdd():void { var resutl:Number = CalculatorLogicHelper.add(2,3); Assert.assertEquals(resutl,5); //Assert.fail("Test method Not yet implemented"); } } }