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:
- Download and insstall flexunit.zip
- http://code.google.com/p/as3flexunitlib/downloads/list

- Extract it (in my case I extracted it to C:\codebase\flex)
- Open Flex Builder 2
- Create a helloFlexUnit project
- In Flex Builder, select File > New> Flex Project,
- Select Basic, and then click Next
- Type helloFlexUnit as the Project Name
- Select your code base location (in my case C:\codebase\flex\helloFlexUnit)

- helloFlexUnit project

- Add flexunit.swc to the library path
- Project/Properties/Flex Build Path/Library Path/Add SWC…
- Select the flexunit.swc file (in my case it is located atC:\codebase\flex\flexunit\bin\flexunit.swc)

- Press OK

- Press OK
- Create SillyTest Unit Test
- Select File > New > ActionScript File (name it SillyTest)
- Add the following code for SillyTest
- // 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);
}
}
}

- Edit helloFlexUnit.mxml
- Add eh following code
- <?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>

- Run SillyTest Unit Test
- 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.