FlexUnit test environment

The FlexUnit test environment allows you to generate and edit repeatable tests that can be run from scripts or directly within Flash Builder. Flash Builder supports both FlexUnit 4 and Flex Unit 1 open source frameworks.

From Flash Builder, you can do the following:

  • Create unit test cases and unit test suites

    Flash Builder wizards guide you through the creation of test cases and test suites, generating stub code for the tests.

  • Run the test cases and test suites

    You can run test cases and test suites various ways from within Flash Builder or outside the Flash Builder environment. The results of the tests are displayed in a test application. Flash Builder opens a Flex Unit Results View for analysis of the test run.

  • Navigate to source code from the Flex Unit Results View

    In the Test Results panel, double-click a test to open the test implementation.

    The Test Failure Details panel lists the source and line number of the failure. If the listed source is in the current workspace, double-click the source to go directly to the failure.

Creating FlexUnit tests

You can create FlexUnit test case classes and test case suites for the following types of projects:

  • Flex project

  • ActionScript project

  • Flex Library project

  • AIR project

When creating a test case class, you can specify the following options:

  • A src folder in a Flex project for the class

  • A package for the class

  • The classes to test

  • Methods to test for each specified class

A FlexUnit test case suite is a series of tests based on previously created test case classes, specified methods in those classes, and other test case suites.

Creating a FlexUnit test case class

When you create a FlexUnit test case class, Flash Builder generates an ActionScript file for the test case class, which it places in a package for test cases.

The following procedure assumes that you have created a project in Flash Builder in which you want to create and run Flex Unit tests.

  1. Select the Flex project and then from the context menu, select New > Test Case Class.

    If you select an ActionScript class file in project, then that class is automatically selected for the FlexUnit test case in the New Test Case Class wizard.

  2. In the New Test Case Class wizard, specify whether to create a class in the FlexUnit 4 style or FlexUnit 1 style.

  3. Specify a name for the test case class.

  4. (Optional) Specify a source folder and package for the test case class, or accept the defaults.

    The default source folder is the src folder of the current project. The default package is flexUnitTests, which is at the top level of the default package structure for the project.

  5. (Optional) Enable the Select Class to Test toggle, and browse to a specific class. Click Next.

  6. (Optional) Select the methods in the selected class that you want to test.

  7. Click Finish.

    Code the test case you created. Use the generated code stubs as a starting point.

Creating a FlexUnit test case suite

This procedure assumes that you have previously created test case classes.

  1. Select the Flex project and then create a test case suite from the context menus by selecting New > Test Suite Class.

  2. In the New Test Suite Class wizard, specify whether to create a class in the FlexUnit 4 style or FlexUnit 1 style.

  3. Provide a name for the test suite.

  4. Navigate in the test suites and test cases to select the classes and methods to include in the test suite. Click Finish.

Customizing default FlexUnit test case classes and test case suite classes

You can customize the default FlexUnit test case classes and test case suite classes that are created by Flash Builder. Flash Builder uses file templates to create the default versions of these files.

The file templates for FlexUnit are available from the Preferences window at Flash builder > File Templates > FlexUnit. There are separate templates for FlexUnit1 and FlexUnit4 test case classes and test suite classes.

See Customizing File Templates for information on how to modify the default file templates.

Note: FlexUnitCompilerApplication.mxml and FlexUnitApplication.mxml derive from the template for MXML Web Application or MXML Desktop Application. The template that is used depends on whether the Flex project is configured for a web application (runs in Adobe® Flash® Player) or a desktop application (runs in Adobe AIR®).

Running FlexUnit tests

FlexUnit tests can be run from within Flash Builder or from outside Flash Builder using SWFs generated for the FlexUnit test. In either case, the results of the tests are displayed in the FlexUnit Results View.

You can also configure and save a FlexUnit test before running it.

By default, FlexUnit tests run in the Flash Debug perspective. You can launch tests from the Flash Development or Flash Profile perspectives, but Flash Builder switches to the Flash Debug perspective when running the test.

You can modify the default perspective for FlexUnit tests. Open the Preferences window, and navigate to Flash Builder > FlexUnit.

FlexUnit compiler application and FlexUnit application

When you create a FlexUnit test case, Flash Builder creates the following FlexUnit compiler application and a FlexUnit application:

  • FlexUnitCompilerApplication.mxml

  • FlexUnitApplication.mxml

Flash Builder uses these applications when compiling and running FlexUnit tests. Flash Builder places the applications, in the src directory of the project.

This application contains references to all FlexUnit test cases and test suites generated by Flash Builder. Flash Builder places all FlexUnit tests in the <fx:Declarations> tag of this application. You typically do not edit or modify this file directly.

Refresh the FlexUnit compiler application for the following circumstances:

  • You manually add a test case.

    If you create a test case class without using the New Test Case wizard, refresh FlexUnitCompilerApplication.mxml. Place the new test case in the package with the other test cases.

  • You rename a test case

  • You delete a test case

Refresh FlexUnitCompilerApplication.mxml:

  1. If the FlexUnit Results view is not open, select Windows > Other Views > FlexUnit Results. Click OK.

  2. In the FlexUnit Results view, click the Refresh button.

Run a FlexUnit test within Flash Builder

You can run FlexUnit tests within Flash Builder on a project level or for individual test cases.

You typically run FlexUnit tests from the context menu for a project or from the context menus for an individual test case.

However, you can also run FlexUnit tests from the Flash Builder Run menu, the Flash Builder Run button, or from the Execute FlexUnit Test button in the FlexUnit Results view.

If you run tests from the Flash Builder Run menu, a Test Configuration dialog opens, allowing you to select which test classes and methods to run. Test cases for library projects cannot be run using the Flash Builder Run menu.

Flash Builder provides the following keyboard shortcuts to quickly launch FlexUnit tests:

  • Alt+Shift+A, F

    Runs all FlexUnit tests in the project.

  • Alt+Shift+E, F

    Runs the selected FlexUnit test.

Run FlexUnit tests from the current selection in the editor. (See Configuring FlexUnit tests.

  1. Select a project and run the test:

    From the context menu for a project, select Execute FlexUnit Tests.

    From the Flash Builder Run menu or Run button, select Run > FlexUnit Tests.

  2. (Flash Builder Run menu) In the Run FlexUnit Tests configuration dialog, select the test cases and methods to run in the test. Click OK to run the tests.

  3. View the test results.

    Flash Builder generates a SWF file in the bin-debug folder of the project.

    An application opens displaying information about the test and indicates when the test is complete.

    The FlexUnit Results View panel opens displaying the results of the test. See Viewing results of a FlexUnit test run.

Run individual FlexUnit tests:

  1. In the Project Explorer, navigate to the flexUnitTest package:

    From the context menu for a FlexUnit test file, select Execute FlexUnit Tests.

  2. View the test results.

    Flash Builder generates a SWF file in the bin-debug folder of the project.

    An application opens displaying information about the test and indicates when the test is complete.

    The FlexUnit Results View panel opens displaying the results of the test. See Viewing results of a FlexUnit test run.

Run a FlexUnit test outside the Flash Builder environment

This procedure assumes that you have previously run a FlexUnit test within Flash Builder and that Flash Builder is running.

  1. Copy the generated SWF file for a test from the bin-debug folder in your project to a folder outside your development environment.

    You can copy the automatically generated SWF file or a SWF file from a FlexUnit test that you have previously saved and configured.

  2. Run the copy of the SWF file.

    Flash Player opens displaying information about the test and indicates when the test is complete.

    The FlexUnit Results View panel opens in Flash Builder displaying the results of the test.

Configuring FlexUnit tests

  1. Open the Run FlexUnit Tests configuration dialog:

    You can open the Run FlexUnit Tests configuration dialog from the Run menu or from the FlexUnit Results view.

    • Select a project. From the Flash Builder menu, select Run > Run > Execute FlexUnit Test.

    • From the FlexUnit Results view, select the Execute FlexUnit Tests button.

      If the FlexUnit Results view is not open, select Window > Other Views > Flash Builder > FlexUnit Results.

  2. In the Test Configuration dialog, select the test cases and methods to save as a test configuration.

    Note: The Test Configuration dialog is not available when you run a test from the context menu in the Package Explorer.
  3. (Optional) Select Load to import previous configurations saved as XML files.

  4. Click Save.

    Flash Builder writes an XML file and an MXML file in the .FlexUnitSettings folder of your project.

    You can use the XML file in build scripts to execute the test.

    You can generate a SWF file from the MXML file. This SWF file can be used for testing outside the Flash Builder environment. Typically, you copy the generated MXML file to the src folder in your project to generate the SWF file.

Viewing results of a FlexUnit test run

The FlexUnit Results view displays results of a FlexUnit test, detailing any failures. You can navigate through the results, filter the display, write the results to a file, and load the results from a file.

You can also rerun tests, cancel a running test, and clear the results from the view.

If the FlexUnit Results view is not open, select Window > Other Views > Flash Builder > FlexUnit Results.

Test Results Panel

This panel lists all tests within the test run, indicating whether the test passed or failed.

Double-click a test in the list to go to the test in the ActionScript editor.

Test Failure Details Panel

Select a test in the Test Results panel to display failure details.

Each failure detail lists the source file and method, including the line number of the failure.

If the source file is local to the working space, double-click the listing to go to the failure in the ActionScript editor.

FlexUnit Results view menu

From the FlexUnit Results view menu, you can do the following:

  • Filter the results displayed

    Hide the Test Failure Details panel.

    Display only test runs that failed.

  • Scroll through the test runs displayed in the Test Results panel.

  • Cancel a running test.

  • Save the results of a test run or the configuration of a test run.

  • Load results previously saved to a file.

  • Clear the results from the panel.

  • Rerun the current test. You can choose from:

    • Run all tests.

    • Run failures only.

    • Run the selected test.

  • Refresh the FlexUnit configuration.

    If you modify a test or add or remove tests, click refresh to load the new FlexUnit configuration.

  • Configure and run FlexUnit tests.

    Use the Execute FlexUnit Tests button to configure and run FlexUnit tests.