Flash Builder 4對Flex開發周期中的FlexUnit集成提供了極好的內置支持。
Create a Test Case Class
可以使用Flash Builder創建測試類嚮導創建測試用例類。正如您將在本文中看到的,使用Flash Builder運行測試用例是輕而易舉的事情。
要使用Flash Builder創建測試用例類,請單擊「新建測試用例類」。輸入如下所示的詳細信息。
Flash Builder將創建以下TestClass1.as文件。
package com.tutorialspoint.client { public class TestClass1 { [Before] public function setUp():void {} [After] public function tearDown():void {} [BeforeClass] public static function setUpBeforeClass():void {} [AfterClass] public static function tearDownAfterClass():void {} } }
FlexUnit Integration Example
現在,讓我們按照步驟在Flex應用程式中測試FlexUnit集成;
Step | Description |
---|---|
1 | Create a project with a name HelloWorld under a package com.tutorialspoint.client as explained in the Flex - Create Application chapter. |
2 | Modify HelloWorld.mxml as explained below. Keep rest of the files unchanged. |
3 | Create TestClass1.as test case as described above and Modify TestClass1.as as explained below. |
4 | Compile and run the application to make sure business logic is working as per the requirements. |
package com.tutorialspoint.client { import org.flexunit.asserts.assertEquals; public class TestClass1 { private var counter: int = 1; [Before] public function setUp():void { //this code will run before every test case execution } [After] public function tearDown():void { //this code will run after every test case execution } [BeforeClass] public static function setUpBeforeClass():void { //this code will run once when test cases start execution } [AfterClass] public static function tearDownAfterClass():void { //this code will run once when test cases ends execution } [Test] public function testCounter():void { assertEquals(counter, 1); } } }
<?xml version = "1.0" encoding = "utf-8"?> <s:Application xmlns:fx = "http://ns.adobe.com/mxml/2009" xmlns:s = "library://ns.adobe.com/flex/spark" xmlns:mx = "library://ns.adobe.com/flex/mx" minWidth = "500" minHeight = "500"> </s:Application>
完成所有更改後,讓我們以正常模式編譯,就像在flex-create application一章中那樣。
Running Test cases
現在右鍵單擊package explorer中的TestClass1並選擇Run As>FlexUnit Tests。您將在Flash Builder測試窗口中看到以下輸出。
Flash Builder還會在瀏覽器中顯示測試結果。