FlexUnit moved to Adobe Open Source and it is similar in functionality to JUnit, it allows us to create test suite and test unit. Adobe are showing their commitment to TDD as best practices for dynamic development cycles. In fact, Flex 4 has built-in menu to generate Unit Testing and Test Suite as well as plugin to view results, until Flex 4 is out here’s an example of creating a test suite and test unit using best practices in an easy to understand example.
The TDD process consists of the following steps:
- Add test
- Write failed unit test
- Write code
- Test Passed
- Refactor
- Repeat
First create the scaffolding with the test runner, test suite and test case than start the TDD process listed above. The complete working example can be seen here.
Let’s take a look at the classes:
the test suite holds all the test cases:
/*
Copyright (c) 2008 Elrom LLC. All Rights Reserved
@author Elad Elrom
@contact elad.ny at gmail.com
@project Project Name
@internal
*/
package flexUnitTests
{
import flexunit.framework.TestSuite;
public class TestSuiteClass extends TestSuite
{
/**
* Class constructor. If you provide a contstructor in a <code>TestCase</code> subclass,
* you should ensure that this constructor is called.
*
* @param param The name of the test method to be called in the test run.
*
*/
public function TestSuiteClass(param:Object=null)
{
super(param);
}
/**
* Holds the test to be run.
*
* @return new instance of the <code>TestSuite</code>
*
*/
public static function suite():TestSuite
{
var newTestSuite:TestSuite = new TestSuite();
return newTestSuite;
}
}
}
The test case that holds the tests for the logic methods:
/*
Copyright (c) 2008 Elrom LLC. All Rights Reserved
@author Elad Elrom
@contact elad.ny at gmail.com
@project Project Name
@internal
*/
package flexUnitTests
{
import com.elad.view.LogicClass;
import flexunit.framework.TestCase;
public class TestCaseClass extends TestCase
{
/**
* A contstructor to pass the method name.
*
* @param methodName The name of the test method to be called in the test run.
*
*/
public function TestCaseClass(methodName:String=null)
{
super(methodName);
}
/**
* First test method to do something
*
*/
public function testFirstMethod():void
{
var logic:LogicClass = new LogicClass();
assertEquals( "Expecting zero here", 0, logic.firstMethod() );
}
/**
* Second test method to do something
*
*/
public function testNextMethod():void
{
var logic:LogicClass = new LogicClass();
assertTrue( "Expecting true here", logic.NextMethod()==50 );
}
}
}
And here’s our logic class:
/*
Copyright (c) 2008 Elrom LLC. All Rights Reserved
@author Elad Elrom
@contact elad.ny at gmail.com
@project Project Name
@internal
*/
package com.elad.view
{
public class LogicClass
{
public function LogicClass()
{
// contstructor
}
public function firstMethod():Number
{
return 0;
}
public function NextMethod():Number
{
return 50;
}
}
}
Last we need to have a test runner component. Create an instance of the test suite and add all the case unit we would like to run:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:flexunit="flexunit.flexui.*"
creationComplete="onCreationComplete()"
layout="absolute">
<mx:Script>
<![CDATA[
import flexUnitTests.TestCaseClass;
import flexUnitTests.TestSuiteClass;
import flexunit.framework.TestSuite;
private function onCreationComplete():void
{
testRunner.test = currentRunTestSuite();
testRunner.startTest();
}
public function currentRunTestSuite():TestSuite
{
var testsToRun:TestSuite = new TestSuite();
testsToRun.addTest(TestSuiteClass.suite());
testsToRun.addTest(new TestCaseClass("testFirstMethod"));
testsToRun.addTest(new TestCaseClass("testNextMethod"));
return testsToRun;
}
]]>
</mx:Script>
<flexunit:TestRunnerBase id="testRunner"/>
</mx:Application>
At this point you can add custom setUp() and tearDown() stubs as well as other test methods and test suites. The complete application can be seen and downloaded (right click ‘view source’), click the image below:




















And now with Flex4, an option is to use the upcoming FlexUnit4 support. Nicely integrated with Flash Builder 4. From within the EDI, you can run a Test case without bothering with test suites.
A FlexUnit4 powered usecase : here.