---
title: "小试FlexUnit(Flex单元测试)"
author: "flash7783"
date: "2010-08-10T21:22:25Z"
tags: ["ActionScript", "Adobe", "Flex", "OpenSource", "单元测试"]
categories: ["研发管理"]
slug: "flexunit-flex"
draft: false
lastmod: "2010-08-10T21:22:25Z"
id: 734039
url: "https://flash7783.iteye.com/blog/734039"
---

最近学习flex，本人是从C/S架构过来的，用起来有很多的熟悉感，为了公司后续的开发工作顺利开展，了解了flex单元测试，感觉还是很不错的，费话不说了，过程如下：

1.下载FlexUnit 0.9  
[http://opensource.adobe.com/wiki/display/flexunit/Downloads](http://opensource.adobe.com/wiki/display/flexunit/Downloads)

2.解压后有两个目录，asDoc是API文档，Bin是SWC库  
![](http://flash7783.iteye.com/upload/attachment/66652/f2c4e4f2-55af-3ec1-a2b3-4c41a8b96e55.jpg)

3.新建项目，如下图设定，按“Finish”新建项目

![](http://flash7783.iteye.com/upload/attachment/66655/e141843c-ed1b-3464-854a-32298d328e31.jpg)

4.右键项目TestFlexUnit-&gt;Properties-&gt;Flex Build Path，点击“Add Swc…”，加入flexUnit的swc包。  
![](http://flash7783.iteye.com/upload/attachment/66656/699ed30a-1ed0-396a-8699-c53766f22837.jpg)

5.新建一个需要测试的业务逻辑类（ActionScript类），例下：

![](http://flash7783.iteye.com/upload/attachment/66657/6e3e0985-566e-3a68-8815-c40ebd12436d.jpg)

Java代码 [![复制代码](http://www.iteye.com/images/icon_copy.gif)](http://www.iteye.com/topic/311480 "复制代码")

01. **package**
02. {
03. **public** **class** Arithmetic
04. {
05. /\**
06. * 加法
07. * author:LiuYX
08. \*/
09. **public** **static** function add(a:Number,b:Number):Number{
10. **return** a + b;
11. }
12. /\**
13. * 减法
14. * author:LiuYX
15. \*/
16. **public** **static** function subtract(a:Number,b:Number):Number{
17. **return** a - b;
18. }
19. }
20. }

```
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类，如下：

Java代码 [![复制代码](http://www.iteye.com/images/icon_copy.gif)](http://www.iteye.com/topic/311480 "复制代码")

01. **package**
02. {
03. **import** flexunit.framework.TestCase;
04. **import** flexunit.framework.TestSuite;
05. **public** **class** ArithmeticTest **extends** TestCase
06. {
07. /\**
08. * 重写TestCase的构造器
09. * author:LiuYX
10. * date:2009-01-07
11. \*/
12. **public** function ArithmeticTest(methodName:String)
13. {
14. //回调TestCase的构造器
15. **super**(methodName);
16. }
17. /\**
18. * 静态方法，返回这个TestCase包含的TestSuite
19. * author:LiuYX
20. * date:2009-01-07
21. \*/
22. **public** **static** function Suite():TestSuite{
23. var suite:TestSuite = **new** TestSuite();
24. suite.addTest(**new** ArithmeticTest("testAdd"));
25. suite.addTest(**new** ArithmeticTest("testSubtract"));
26. **return** suite;
27. }
28. /\**
29. * TestCast的测试方法，测试业务类的加法逻辑
30. * author:LiuYX
31. * date:2009-01-07
32. \*/
33. **public** function testAdd():**void**{
34. var result:Number = 0;
35. result = Arithmetic.add(1,2);
36. assertTrue("和为 3",result == 3);
37. result = Arithmetic.add(2,2);
38. assertEquals("和为 4",4,result);
39. }
40. /\**
41. * TestCast的测试方法，测试业务类的减法逻辑
42. * author:LiuYX
43. * date:2009-01-07
44. \*/
45. **public** function testSubtract():**void**{
46. var result:Number = 0;
47. result = Arithmetic.subtract(2,1);
48. assertTrue("结果为 1",result == 1);
49. assertTrue("结果为 0",0,Arithmetic.subtract(2,2));
50. }
51. }
52. }

```
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。

Java代码 [![复制代码](http://www.iteye.com/images/icon_copy.gif)](http://www.iteye.com/topic/311480 "复制代码")

01. &lt;?xml version="1.0" encoding="utf-8"?&gt;
02. &lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
03. xmlns:flexunit="flexunit.flexui.\*"
04. layout="absolute"
05. creationComplete="onCreationComplete()"&gt;
06. &lt;mx:Script&gt;
07. &lt;![CDATA[
08. **import** flexunit.framework.TestSuite;
09. /\**
10. * 创建整个项目的TestSuite
11. * author:LiuYX
12. \*/
13. **private** function createSuite():TestSuite
14. {
15. /\*组建TestSuite\*/
16. var suite:TestSuite = **new** TestSuite();
17. suite.addTest(ArithmeticTest.Suite());
18. /\*TODO:在这加入其它TestSuite\*/
19. **return** suite;
20. }
21. /\**
22. * 调用TestRunner进行测试
23. \*/
24. **private** function onCreationComplete():**void**
25. {
26. /\*代入TestRunnerBase进行测试\*/
27. testRunner.test = **this**.createSuite();
28. testRunner.startTest();
29. }
30. ]]&gt;
31. &lt;/mx:Script&gt;
32. &lt;!-- flexunit的TestRunnerBase界面组件 --&gt;
33. &lt;flexunit:TestRunnerBase id="testRunner" width="100%" height="100%" /&gt;
34. &lt;/mx:Application&gt;

```
<?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>
```

运行结果：

![](http://flash7783.iteye.com/upload/attachment/66661/72d6b645-2261-3d04-a3eb-b84fa25553db.jpg "点击查看原始大小图片")

- [![](http://dl.iteye.com/upload/attachment/66652/f2c4e4f2-55af-3ec1-a2b3-4c41a8b96e55-thumb.jpg "点击查看原始大小图片")](http://dl.iteye.com/upload/attachment/66652/f2c4e4f2-55af-3ec1-a2b3-4c41a8b96e55.jpg)
- 大小: 8.4 KB

<!--THE END-->

- [![](http://dl.iteye.com/upload/attachment/66655/e141843c-ed1b-3464-854a-32298d328e31-thumb.jpg "点击查看原始大小图片")](http://dl.iteye.com/upload/attachment/66655/e141843c-ed1b-3464-854a-32298d328e31.jpg)
- 大小: 44.4 KB

<!--THE END-->

- [![](http://dl.iteye.com/upload/attachment/66656/699ed30a-1ed0-396a-8699-c53766f22837-thumb.jpg "点击查看原始大小图片")](http://dl.iteye.com/upload/attachment/66656/699ed30a-1ed0-396a-8699-c53766f22837.jpg)
- 大小: 55.8 KB

<!--THE END-->

- [![](http://dl.iteye.com/upload/attachment/66657/6e3e0985-566e-3a68-8815-c40ebd12436d-thumb.jpg "点击查看原始大小图片")](http://dl.iteye.com/upload/attachment/66657/6e3e0985-566e-3a68-8815-c40ebd12436d.jpg)
- 大小: 36.4 KB

<!--THE END-->

- [![](http://dl.iteye.com/upload/attachment/66661/72d6b645-2261-3d04-a3eb-b84fa25553db-thumb.jpg "点击查看原始大小图片")](http://dl.iteye.com/upload/attachment/66661/72d6b645-2261-3d04-a3eb-b84fa25553db.jpg)
- 大小: 34 KB

<!--THE END-->

- [查看图片附件](http://flash7783.iteye.com/admin/blogs/)