当前位置: 首页 > 工具软件 > FlexUnit > 使用案例 >

FlexUnit 的用法

姚煜
2023-12-01

FlexUnit 是一个开源的AS3单元测试框架,这样用:

1.先下载 FlexUnit,在附件当中。
2.解开压缩包
3.新建一个 Flex Project(Basic那种),在类路径配置当中点击“Add SWC”, 选择解开的压缩包当中的
./bin/flexunit.swc(截图请看附件当中的”图一“)
然后点击 finish
4.写一个入口类:FlexUnitEntry.as,取别的名字也可以。只要和 MXML 当中对应就行


CODE:[Copy to clipboard]package {
        import flexunit.framework.TestSuite;

       
        public class FlexUnitEntry {
                public static function suite():TestSuite {
                           var ts:TestSuite = new TestSuite();
                          
                           //To-do:在这里加入你要测试的测试用例
                          
                           return ts;
                   }
        }
}
4.在 项目的 MXML 当中这样写:


CODE:[Copy to clipboard]<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*"
                                xmlns:flexunit="flexunit.flexui.*"
                                creationComplete="onCreationComplete()">
        <mx:Script>
                <![CDATA[
                        import flexunit.framework.TestSuite;
                       
                        private function onCreationComplete() :void {
                                this.runnerBase.test = this.suite();
                                this.runnerBase.startTest();
                        }
                       
                        private function suite():TestSuite {
                                var ts:TestSuite = new TestSuite();
                                ts.addTest(FlexUnitEntry.suite());
                                return ts;
                        }
 ]]>
        </mx:Script>

        <!-- flexunit provides a very handy default test runner GUI -->
        <flexunit:TestRunnerBase id="runnerBase" width="100%" height="100%" />

</mx:Application>
5.写一个测试类,这里建议用这样的模版: CODE:[Copy to clipboard]package {
        import flexunit.framework.TestCase;
        import flexunit.framework.TestSuite;
        import flexunit.framework.Assert;

        public class MyFlexUnitTest extends TestCase {
               
                public static function suite() :TestSuite {
                        var ts:TestSuite = new TestSuite();
                       
                        ts.addTest(new MyFlexUnitTest("testAssertTrue"));//用要测试的方法名作为构造函数参数传入
                        ts.addTest(new MyFlexUnitTest("testAssertEquals"));
                       
                        return ts;
                }
               
                public function MyFlexUnitTest(methodName:String=null) {
                        super(methodName);
                }
               
                //方法名不是像JUnit那样必须用 test.... 开头,不过最好这样写
                public function testAssertTrue() :void {
                        Assert.assertTrue(false);//肯定报错
                }
               
                public function testAssertEquals() :void {
                        Assert.assertEquals(1.0, 1.0);
                }
        }
}
6.在入口类(FlexUnitEntry)当中加入这个测试用例,这样:


CODE:[Copy to clipboard]public static function suite():TestSuite {
        var ts:TestSuite = new TestSuite();

        //To-do:在这里加入你要测试的测试用例                       
        ts.addTest(MyFlexUnitTest.suite());
                          
        return ts;
                   }
7.运行项目的 MXML 就看到结果了(建议用Debug模式运行)。如果是绿条,说明所有测试通过,如果是红的,说明有错误。会有显示。
项目编译的时候建议设置 -default-size 600 440 这样FlexUnit 的界面显示就比较完全了。

 

本文转自:http://www.5uflash.com/Flex-AIR/Flexziliao/1767_3.html

 类似资料: