helloopensource

 

FlexUnit

Page history last edited by Paulo 1 yr ago

 

 Hello FlexUnit

This Tutorial will show you how to write a simple ActionScript Unit Test Case using FlexUnit.
FlexUnit is a unit testing framework for Flex and ActionScript 3.0 applications. It mimics the functionality of JUnit, a Java unit testing framework, and comes with a graphical test runner.

 

Main Learning's

After following this tutorial you will learn how to:

 

  • Create a FlexUnit Test Case
  • Add FlexUnit library to a Flex project build path
  • Run a FlexUnit Test Case
 

Versions used:

  • Flex Builder 2
  • ActionScript 3.0
  • FlexUnit 0.85

 

Assumptions: 

Flex Builder 2 is properly installed in your machine. 

Hello Flex Builder 2 tutorial shows how to download and install flex Builder 2.

Hello ActionScript tutorial shows a Hello World for ActionScript.

 

Tutorial Steps:

  1. Download and insstall flexunit.zip
    1.  http://code.google.com/p/as3flexunitlib/downloads/list
    2. Extract it (in my case I extracted it to C:\codebase\flex)
  2. Open Flex Builder 2
  3. Create a helloFlexUnit project
    1. In Flex Builder, select File > New> Flex Project,
    2. Select Basic, and then click Next
    3. Type helloFlexUnit as the Project Name
    4. Select your code base location (in my case C:\codebase\flex\helloFlexUnit)
    5. helloFlexUnit project
  4. Add flexunit.swc to the library path
    1. Project/Properties/Flex Build Path/Library Path/Add SWC…
    2. Select the flexunit.swc file (in my case it is located atC:\codebase\flex\flexunit\bin\flexunit.swc)
    3. Press OK
    4. Press OK
  5. Create SillyTest Unit Test
    1. Select File > New > ActionScript File (name it SillyTest)
    2. Add the following code for  SillyTest
    3. // ActionScript file

      package

      {

          import flexunit.framework.TestCase;

          import flexunit.framework.TestSuite;

       

          public class SillyTest extends TestCase

          {

              public function SillyTest(methodName : String){

                  super(methodName);

              }

       

              public static function suite():TestSuite{

                  var myTS:TestSuite = new TestSuite();

                  myTS.addTest(new SillyTest("testTheTruth"));

                  myTS.addTest(new SillyTest("testSum"));

                  return myTS;

              }

       

              public function testTheTruth():void{

                  assertTrue(true);

              }

       

              public function testSum():void{

                  assertEquals(3, 2+1);

              }                

          }

      }

  6. Edit helloFlexUnit.mxml
    1. Add eh following code
    2. <?xml version="1.0" encoding="utf-8"?>

      <mx:Application

          xmlns:mx="http://www.adobe.com/2006/mxml"

          xmlns:flexunit="flexunit.flexui.*"

          creationComplete="onCreationComplete()"

      >   

          <mx:Script>

              <![CDATA[

       

                  private function onCreationComplete():void

                  {

                      testRunner.test = SillyTest.suite();

                      testRunner.startTest();

                  }

              ]]>

          </mx:Script>   

          <flexunit:TestRunnerBase id="testRunner" width="100%" height="100%" />

      </mx:Application>

  7. Run SillyTest Unit Test
    1. Run/Run/helloFlexUnit

     

    Why is this unit test silly?

    You don’t need to test the ActionScript language (true, 1+2=3). Instead your unit tests should be testing functional code that you write.

     

 

 

Comments (0)

You don't have permission to comment on this page.