`

Create a FlexUnit TestCase

    博客分类:
  • flex
阅读更多

 

Problem

How to create a FlexUnit TestCase class that can be used to test code.

怎么创建能用于测试代码的flex单元测试用例

Solution

Create a class that extends TestCase and include one or more public methods whose name starts with test.

创建一个类继承TestCase,类中包括一或者多个以test开头的公共的方法

Detailed explanation

1. Create an ActionScript class that extends TestCase

The standard convention is to name the TestCase class after the class being tested with Test added as a suffix. For example if the class to be tested was called RegExp the TestCase class would be called RegExpTest. Additionally by convention the TestCase class is placed in the same package as the class under test. For example if the class was mx.core.UITextFormat the TestCase class would be mx.core.UITextFormatTest.

Create a new ActionScript class that extends TestCase. The remainder of this recipe will use the following RegExpTest as the example:
package
{
    import flexunit.framework.TestCase;

    public class RegExpTest extends TestCase
    {
    }
}

2. Create a method whose name starts with test

The FlexUnit framework uses reflection to determine the methods in a TestCase that should be run. Starting function names with test indicates to FlexUnit that the function should be included in those that are run.

Add the following method to RegExpTest:
        public function testRegExp():void
        {
        }

3. Make one or more assertions

An assertion is a programmatic way of verifying a statement of fact. The most common form is comparing an expected value to the actual value returned by some operation. FlexUnit includes a number of assertion types that can be used to test different situations. The most common assertions are:
  • assertEquals: Compare with ==
  • assertTrue: Check condition is true
  • assertNull: Check if null
  • assertStrictlyEquals: Compare with ===

FlexUnit also provides various convenience assertions which test for the opposite condition such as assertFalse and assertNotNull. See the Assert API documentation included with FlexUnit for a complete list of assertions.

Each assertion function can take an optional String as the first argument which will be included in the assertion output should the assertion fail. This String will be prefixed before the default "expected X but was Y" failure message.

When writing assertions keep in mind that if an assertion fails, the rest of the test method will not be executed.

TestCase extends Assert which defines all of the assert functions. This allows subclasses of TestCase to directly call the assert functions. The following code demonstrate the various assertion methods and should be added to the testRegExp function:

            var regExp:RegExp = new RegExp("a", "i");
            assertFalse(regExp.test("b"));
            assertFalse("regExp doesn't match", regExp.test("b"));
            
            assertNull(regExp.exec("b"));
            assertNull("regExp doesn't match", regExp.exec("b"));
            
            assertNotNull(regExp.exec("Apple"));
            assertNotNull("regExp matches", regExp.exec("Apple"));

            assertTrue(regExp.exec("Apple") is Array);
            assertTrue("regExp exec returned an Array", regExp.exec("Apple") is Array);
            
            assertEquals("A", regExp.exec("Apple")[0]);
            assertEquals("regExp matched A in Apple", "A", regExp.exec("Apple")[0]);
            
            assertStrictlyEquals(regExp, regExp);
            assertStrictlyEquals("regExp object identity", regExp, regExp);

 

4. Add additional test methods


Add new test methods to the TestCase to test other logical groups of operations. It is convention that each test method focus on testing a specific operation or task. For example when testing create, retrieve, update, and delete operations each one should be put into its own test method such as testCreate, testRetrieve, etc. This way should assertions start to fail, multiple failures will be reported helping diagnosis the issue.

It is important to keep in mind that the order that the test methods in a TestCase are run is random. Each test should create its own data and make no assumptions about another test having already run.

5. Add the TestCase to the TestSuite


The last step is to add the newly created TestCase to the TestSuite.
See the Adding a TestCase to a TestSuite recipe.

Reference

The final ActionScript file:

package
{
    import flexunit.framework.TestCase;

    public class RegExpTest extends TestCase
    {
        public function testRegExp():void
        {
            var regExp:RegExp = new RegExp("a", "i");
            assertFalse(regExp.test("b"));
            assertFalse("regExp doesn't match", regExp.test("b"));
            
            assertNull(regExp.exec("b"));
            assertNull("regExp doesn't match", regExp.exec("b"));
            
            assertNotNull(regExp.exec("Apple"));
            assertNotNull("regExp matches", regExp.exec("Apple"));

            assertTrue(regExp.exec("Apple") is Array);
            assertTrue("regExp exec returned an Array", regExp.exec("Apple") is Array);
            
            assertEquals("A", regExp.exec("Apple")[0]);
            assertEquals("regExp matched A in Apple", "A", regExp.exec("Apple")[0]);
            
            assertStrictlyEquals(regExp, regExp);
            assertStrictlyEquals("regExp object identity", regExp, regExp);
        }
    }
}
分享到:
评论

相关推荐

    flexunit-4.1.0_RC2-4-4.1.0.16076.zip,flexunit 4

    - **创建测试类**:为每个要测试的类创建一个相应的测试类,继承自`flexunit4.framework.TestCase`。 - **编写测试方法**:使用`@Test`注解标记测试方法,并在其中使用断言库来验证代码行为。 - **运行测试**:...

    flexunit 详细简单用例里面带有flexunit.swc包

    2. **创建测试类**:接着,创建一个继承自`flexunit.framework.TestSuite` 或 `flexunit.framework.TestCase` 的新类。在这个类中,你需要定义测试方法,这些方法通常以`test` 开头,比如`testAddition`。 3. **...

    FlexUnit,Flex调试专用插件!

    1. **创建测试类**:在Flash Builder中,可以使用模板快速创建测试类,测试类需要继承自FlexUnit的TestCase类。 2. **编写测试代码**:在测试类中,编写具体的测试方法,每个方法代表一个测试用例,使用断言方法来...

    FlexUnit4熟悉与使用

    创建测试用例时,你可以创建一个继承自`flexunit.framework.TestCase`的类,并在其中定义测试方法。测试方法通常以`test`开头,这些方法内部会调用`assertEquals()`、`assertTrue()`、`assertFalse()`等断言方法来...

    FlexUnit4.1

    Flex当前炙手可热的RIA技术,FlexUnit使Flex开发的单元测试成为可能。FlexUnit的目标和思想与JUnit都是差不多的,在此不赘述。FlexUnit也经历了几个版本,目前最新的稳定版是1.0RC版,已经支持了标注式的test,非常...

    flexunit ant版本里面有ant的详细配置

    FlexUnit和Ant是两个在Java和ActionScript开发中常用的工具。FlexUnit是一个单元测试框架,用于测试ActionScript和Flex应用程序,而Ant是一个开源的构建工具,它使用XML来定义项目构建过程,包括编译、打包、测试等...

    flexunit-server:Flexunit Node.js 服务器

    flexunit-服务器FlexUnit Node.js 服务器。 该模块接受来自 FlexUnit CIListener 的连接并使用给定的报告器生成结果。使用示例 var fuserver = require ( ... createServer ( reporter ) ;server . listen ( 8080 ) ;

    flex-unit包

    9. **FlexUnit4AirCIListener** 和 **FlexUnit4FlexCoverListener**:这两个文件很可能分别是FlexUnit测试框架的监听器,用于在AIR (Adobe Integrated Runtime)环境中和代码覆盖率分析中增强测试体验。监听器可以...

    grunt-flexunit:flexunit 服务器的 Grunt 插件

    grunt-flexunit flexunit-server 的 Grunt 插件。 额外要求 除了 npm 模块依赖项之外,运行grunt-flexunit还需要以下内容。 要用于打开swf文件的浏览器 目前仅支持Firefox 浏览器的 Flash Player 插件 xvfb-run ...

    grunt-flexunit:一个Grunt任务插件,可为基于Adobe FlexActionScriptMXMLFlashAIRetc的应用程序运行FlexUnit测试

    咕flex挠单位一个Grunt任务插件,用于为基于Adobe Flex / ActionScript / MXML / Flash / AIR / etc的应用运行FlexUnit测试。入门这个插件需要~0.4.2 如果您以前从未使用过 ,请务必查看《指南》,因为它说明了如何...

    FlexUnit-开源

    FlexUnit 是一个开源的单元测试框架,专门设计用于Macromedia Flex(现在称为Adobe Flex)应用程序的开发。这个框架使得ActionScript 2.0开发者能够按照面向对象的方式编写测试用例,确保他们的代码质量可靠,功能...

    Flex持续集成之单元测试

    压缩包中的"flexunit-flexunit-4.1.0-2-g7d4c01c.zip"很可能就是FlexUnit 4.1.0的源码或库文件,开发者可以通过导入这个库到项目中,利用其提供的测试类和方法来构建和执行单元测试。 持续集成(Continuous ...

    Screenshot:Screenshot 是一个用于 UI 组件集成测试的 ActionScript 3 实用程序。 它与 FlexUnit 测试流程和 Flex Framework UI 组件完美配合

    它与 FlexUnit 测试流程和 Flex Framework UI 组件完美配合。 Flash 有一项特殊能力——输出看起来总是一样的。 快速、轻便且易于使用 [ Test(async, ui) ] public function defaultColor() : void { var ...

    使用ADOBE FLASH BUILDER 4.5

    第七章主要介绍Flash Builder中的单元测试工具FlexUnit,包括FlexUnit测试环境的创建、FlexUnit测试的创建和运行、配置FlexUnit测试以及查看FlexUnit测试运行的结果。 第八章介绍了使用Flex开发应用程序的基本工作...

    fd-unit:用于单元测试的FlashDevelop 4插件

    `fd-unit` 是一个专为FlashDevelop 4设计的插件,旨在帮助Flash开发者进行单元测试,特别是针对使用FlexUnit4框架的项目。FlashDevelop是一款流行的开源集成开发环境(IDE),主要用来编写ActionScript 3、Flex以及...

    Flex4 快捷键大全

    6. Alt+Shift+A,F:执行活动项目的FlexUnit测试,用于自动化单元测试。 7. Alt+Shift+B:在Breadcrumb中显示,有助于在项目导航中快速定位。 8. Alt+Shift+C:Mark Task Complete,标记任务已完成,便于任务管理。 ...

    spring actionscript 必须的.swc包

    FlexUnit提供了类级别的测试和方法级别的断言,使得开发者能够确保他们的代码按照预期工作,提高了软件质量。 2. **spring-actionscript.swc**:这是Spring ActionScript的核心库,包含框架的主要功能,如依赖注入...

    斗地主单机版

    2. `FlexUnitApplication.as`:FlexUnit是ActionScript的单元测试框架,此文件可能是测试类的入口点,用于确保游戏代码的质量和功能正确性。 3. `.project`:这是Eclipse或Flex Builder等IDE的项目配置文件,包含了...

    flex测试总结

    1. FlexUnit:FlexUnit是Flex版本的JUnit,用于进行单元测试,可以对ActionScript代码进行断言和模拟,确保代码片段的功能正确性。 2. Flex Monkey:这是一款自动化UI测试工具,通过模拟用户操作来检查应用程序的...

Global site tag (gtag.js) - Google Analytics