AS3Unit (日本語はこちら)

Source Code / License / Documentation / ASDoc

AS3Unit

AS3Unit is a Unit Test framework for ActionScript? 3.0. By leveraging a namespace feature, which is introduced in AS3, you can write your unit test in POJO style.

AS3Unit enables you to develop Flash application efficiently in test-driven style.

What's new

  • Apr 15th, 2008 AS3Unit 1.2 for FlexSDK3 is released
  • Sep 17th, 2007 AS3Unit 1.2 is released. Release note(Japanese)
  • Apr 4th, 2007 AS3Unit 1.2 beta1 is released.Release note(Japanese)
  • Mar 9th, 2006 AS3Unit 1.01 is released.

Downloads

Stable version

AS3Unit for Async (beta)

Older versions

How to use

For Flex Builder

  1. In Flex Builder, open existing project or create new one.
  2. Show the project property, and click "Library Path" tab in "ActionScript? build path"
  3. Click "add SWC" button, and specify the path to as3unit-xxx.swc.
  4. Click "OK" button and done. Now you can use AS3Unit (org.libspark.as3unit package) in your project.

For Flash CS3

  1. If you've not installed Adobe Extention Manager, please install it.
  2. Download as3unit-1.2-win.mxp (for Windows) or as3unit-1.2-mac.mxp (for Mac)
  3. Double-click the downloaded .mxp file. Then Extension Manager asks you the path for Configuration folder, so please choose appropriate folder (C:/Program Files/Adobe/Adobe Flash CS3/en/Configuration(Win) or /Applications/Adobe Flash CS3/Configuration(Mac)).
  4. If the instration is successfully done, you can use AS3Unit (org.libspark.as3unit package) in Flash CS3.

AS3Unit Component

From AS3Unit 1.2, we provide AS3Unit component for Flex. If you use this component, you don't have to use AS3Unit GUI in order to visualize a test results.

To use the component, plese make a Flex application like the following one:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
	xmlns:mx="http://www.adobe.com/2006/mxml"
	xmlns:as3unit="org.libspark.as3unit.components.*"
	layout="absolute"
	creationComplete="creationCompleteHandler()">
	
	<mx:Script>
		<![CDATA[
			import your.domain.AllTests
			
			private function creationCompleteHandler():void
			{
				as3unit.run(AllTests);
			}
		]]>
	</mx:Script>
	
	<as3unit:AS3Unit id="as3unit"/>
	
</mx:Application>

Create name space which is targeted to "org.libspark.as3unit.components.*" and use the name space to deploy AS3Unit component in your Flex application. After that, please invoke "run" method, which is in AS3Unit component, in "creationComplete" event hander.

AS3Unit GUI

AS3Unit GUI is made by Adobe AIR. For further info about AS3Unit GUI, please refer wiki.(Japanese)

  1. Before install AS3Unit GUI, install Adobe AIR runtime.
  2. Install as3unit-xxx-gui.air
  3. Now you can use AS3Unit GUI

Required environment for AS3Unit

Getting Started

In this section, we'll introduce how to use AS3Unit by making a test unit that tests Caluculator#add method.

First, create a test. Test methods which are invoked by AS3Unit are modified by "test" name space. Use "assert" methods to make test cases.

package
{
    import org.libspark.as3unit.test;
    import org.libspark.as3unit.assert.assertEquals;
    
    use namespace test;
    
    public class CalculatorTest
    {
        test function add():void
        {
            var calc:Calculator = new Calculator();
            assertEquals(3, calc.add(1, 2));
        }
    }
}

Second, create a class which is tested by the above test case.

package
{
    public class Calculator
    {
        public function add(a:Number, b:Number):Number
        {
            return 0;
        }
    }
}

Third, create a class to run the test.

package
{
    import flash.display.Sprite;
    import org.libspark.as3unit.runner.AS3UnitCore;
    
    public class Main extends Sprite
    {
        public function Main()
        {
            AS3UnitCore.main(CalculatorTest);
        }
    }
}

Next, run the test to confirm that the test is not passed because you havn't implement Caluculator#add yet.

.E
Time: 0.031
There was 1 failure:
1) add(CalculatorTest)
Error: expected:<3> but was:<0>
	at ...

FAILURES!!!!
Tests run: 1, Failure: 1

Then, implement Caluculator#add method to pass the test.

package
{
    public class Calculator
    {
        public function add(a:Number, b:Number):Number
        {
            return a + b;
        }
    }
}

Finally, run the test and confirm that the test is now passed.

.
Time: 0.006

OK (1 test)

For further information, please refer documentation.