FLEXUNIT单元测试
风靡一时的JUNIT测试终于全面铺开,在所有的开发领域现行。例如这个刚刚诞生还不多久的FLEX
呵呵,规范的单元测试带来的好处是毋庸置疑的,我们也衷心的感谢为这样伟大的框架而默默奋斗的CODER&ARCHITECTERS。谢谢他们为程序员的幸福生活提供了保障。
下面就着ADOBE的官方指导推荐下如何进行单元测试的编写。
1.Create an Application that includes TestRunnerBaseCreate an Application that includes TestRunnerBase
创建一个包含测试运行器基类的应用程序:如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:flexui="flexunit.flexui.*">
<flexui:TestRunnerBase id="testRunner" width="100%" height="100%"/>
</mx:Application>
这样做的目的是使测试的结果能够以UI直观的呈现给测试人员,用过基于ECLIPSE的JUNIT的CODER对这点应该有直观的感受。
2.. Create a TestSuite
创建一个测试用例
A TestSuite holds a collection of tests that should be run. This example does not add any tests to the TestSuite instance, it just creates the object that TestCase instances can be added to.
测试用例保存了一些列应该被运行的测试,在这个例子中不会进行任何的测试,仅仅用于演示的目的添加了一个TESTCASE实例
<mx:Script>
<![CDATA[
import flexunit.framework.TestSuite;
private function createTestSuite():TestSuite
{
var testSuite:TestSuite = new TestSuite();
return testSuite;
}
]]>
</mx:Script>
3. Assign the TestSuite instance to the TestRunnerBase instance and start the tests
将新创建的测试套件实例添加到测试运行基类实例然后开始测试。
This assigns the TestSuite instance to the TestRunnerBase instance and starts running the tests. Add code for a handleCreationComplete function to the existing Script block:
下面的代码演示了如何将一个测试套件实例添加到测试运行基类然后运行。这里用到的是创建完成的事件。
private function handleCreationComplete():void
{
testRunner.test = createTestSuite();
testRunner.startTest();
}
这个事件添加完成(注意要在APPLICATION里面添加相应的事件响应处理函数)后,就可以运行相应的测试了。
最后这个工程的代码应该是这个样子的。
<?xml version="1.0" encoding="utf-8"?>
<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="testRunner" width="100%" height="100%"/>
</mx:Application>