[转]Flex unit testflex-4-create-a-simple-unit-test-in-flash-builder
原文:http://www.flashas.net/flex/200906094229.html
原文:http://www.hufkens.net/2009/06/flex-4-create-a-simple-unit-test-in-flash-builder/
Flash Builder(Flex?Builder) 4 Beta集成了flex unit test框架,在flash builder中進行單元測試更加方便了。Flash Builder可以幫助開發人員生成TestCase和TestSuite相關的類框架,開發人員只需要對具體的測試邏輯進行編碼即可。在run菜單里可 以直接運行測試,flash builder會自動尋找相關的測試類,并執行他們。下面圖解創建單元測試的過程。
?
1.創建測試用例(TestCase)
?
2.選擇待測試的類
?
3.選擇要測試的方法
?
4.編寫測試邏輯
?
5.運行測試,回收測試結果
?
Flash Builder 4 beta中集成flex unit test框架后,讓這一框架的集成性得到提高,方便了單元測試開發。
P.S.在新版的builder中,查找(Find in files)的速度得到了極大地提升。很不錯。
One of the new features of Flash Builder is the support for?FlexUnit?(the Unit testing framework for Flex applications). You could use FlexUnit in the past, but as from Flash Builder it’s a piece of cake to create Test classes for your own code. You just need to follow these simple steps.
NOTE: This demo needs the latest beta version of?Flash Builder. Be aware that this is still a beta version and the following could still change in future versions of Flash Builder.
Before we start we need to create a new Flex Project. After creating your project you need to create a class with some functionality that we can use in this example. The typical classes to unit test are classes that have a public interface and have a complicated implementation. When you change something inside the class implementation, the public methods still works as expected. But for the sake of simplicity we will create a very simple class with some simple behaviour.
package net.hufkens?{
??/**
? ?* Class that does something.
? ?*?
? ?* @author alain
? ?*?
? ?*/?
??public?class?MyObject
??{
? ??/**
? ? ?* The constructor?
? ? ?*/???
? ??public?function?MyObject()
? ??{
? ??}
? ??
? ??/**
? ? ?* Returns the numeric vallue five
? ? ?* @return 5
? ? ?*/???
? ??public?function?giveMeFive():int
? ??{
? ? ??return?5;
? ??}
? ??
? ??/**
? ? ?* Returns the letter A
? ? ?* @return "A"
? ? ?*/???
? ??public?function?sayA():String
? ??{
? ? ??return?"B";
? ??}
? ??
? ??/**
? ? ?* Returns always true
? ? ?* @return true
? ? ?*/???
? ??public?function?alwaysTrue():Boolean
? ??{
? ? ??return?true;
? ??}
??}
}
Alright, now we have a Class that we can test. To create a new Unit Test you select?New?>TestCase Class?and the following window should appear:
Enter the?Name?of the TestCase Class (in this case:?MyTestClass), select the Class to test (in this case:?MyObject) and select?Finish. Flash Builder now generates several items. First a new folder name?flexUnitTests?is generated. Also a new mxml file namedflexUnitCompilerApplication.mxml?is generated. This file is necessary because the Unit Tests need a swf application that can be started and the tests can be executed. The last and most important file that’s been generated is the TestCase itself. The only thing you now need to do is implement the actual test methods. In the following code example the test methods are implemented:
package flexUnitTests{
??import?flexunit.framework.Assert;
??import?flexunit.framework.TestCase;
??
??import?net.hufkens.MyObject;
??public?class?MyTestClass?extends?TestCase
??{
? ??// Reference declaration for class to test
? ??private?var?classToTestRef : net.hufkens.MyObject;
? ??
? ??public?function?MyTestClass(methodName:String=null)
? ??{
? ? ??super(methodName);
? ??}
? ??
? ??//This method will be called before every test function
? ? override?public?function?setUp():void
? ??{
? ? ??super.setUp();
? ? ? classToTestRef =?new?MyObject();
? ??}
? ??
? ??//This method will be called after every test function
? ? override?public?function?tearDown():void
? ??{
? ? ??super.tearDown();
? ??}
? ??
? ??public?function?testGiveMeFive():void
? ??{
? ? ? Assert.assertEquals(classToTestRef.giveMeFive(), 5);
? ??}
? ??
? ??public?function?testSayA():void
? ??{
? ? ? Assert.assertEquals(classToTestRef.sayA(),?"A");
? ??}
? ??
? ??public?function?testIsTrue():void
? ??{
? ? ? Assert.assertTrue(classToTestRef.alwaysTrue());
? ??}
??}
}
If we now right-click the project and select?Execute FlexUnit Tests?we get the following message:
It says we need to create a Test Suite, otherwise Flash Builder can’t execute the tests. To create a new Test Suite you select?New?>?TestSuite Class?and the following window should appear:
Now select?Execute FlexUnit Tests?again and your Test are executed. As you can see, one of the tests has failed. A quick view in the source of?sayA()?method reveals the problem. Fix the problem and all the tests should succeed now.
That’s all there is to it. Flash Builder makes it a lot easier to create Unit Tests for your libraries and components. This is a good thing, so you should definitely check it out.
轉載于:https://www.cnblogs.com/sun11086/archive/2009/10/15/1583609.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的[转]Flex unit testflex-4-create-a-simple-unit-test-in-flash-builder的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL Server性能调优入门(图文版
- 下一篇: 中小企业 软交换机 呼叫中心 需要的请留