24.2.运行FlexUnit单元测试

24.2.1. 问题
我需要创建应用程序运行FlexUnit 测试并显示测试结果。
24.2.2. 解决办法
使用TestSuite 实例和TestRunnerBase 组件运行测试。
24.2.3. 讨论
TestRunnerBase 是默认的包含FlexUnit 框架的图形化测试运行器。想要用TestRunnerBase 测试创建的应用程序,编辑MXML 文件,加入如下内容:
+展开
-XML
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:flexui="flexunit.flexui.*">

<flexui:TestRunnerBase id="testRunnerwidth="100%"
height="100%"/>

</mx:Application>

编译并运行程序,输出结果像下面这样Figure 24-1.

Figure 24-1. The initial appearance of a FlexUnit test application

然后,创建测试集合保存所有测试。在同一个MXML 文件中添加<mx:Script>块:
+展开
-XML
<mx:Script>
<![CDATA[
import flexunit.framework.TestSuite;
private function createTestSuite():TestSuite
{
var testSuite:TestSuite = new TestSuite();
return testSuite;
}

]]>
</mx:Script>

注意这个例子并没有添加任何测试到测试集合实例中,它只是创建了对象,以便往此添加TestCase 实例。

最后,通过handleCreationComplete 函数把TestSuite 实例赋值给TestRunnerBase 实例:
+展开
-ActionScript
private function handleCreationComplete():void
{
testRunner.test = createTestSuite();
testRunner.startTest();
}

这个函数将在程序载入时自动启动测试。
+展开
-XML
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:flexui="flexunit.flexui.*creationComplete="handleCreationComplete();">

编译和运行程序后,输出面板如图Figure 24-1,现在你可以添加测试用例(TestCase )实例到TestSuite 实例中,当程序开始时它们将被运行。

这是最终的MXML 文件:
+展开
-XML
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:flexui="flexunit.flexui.*creationComplete="handleCreationComplete();">

<mx:Script>
<![CDATA[
import flexunit.framework.TestSuite;
private function createTestSuite():TestSuite
{
var testSuite:TestSuite = new TestSuite();
return testSuite;
}
private function handleCreationComplete():void
{
testRunner.test = createTestSuite();
testRunner.startTest();
}

]]>
</mx:Script>
<flexui:TestRunnerBase id="testRunnerwidth="100%height="100%"/>
</mx:Application>

加支付宝好友偷能量挖...


评论(0)网络
阅读(74)喜欢(0)flash/flex/fcs/AIR