My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Resources  

Featured
Updated Feb 4, 2010 by mikechambers

This page contains articles, tutorials and other resources on how to work with the FlexUnit ActionScript Unit Testing framework.

As of August 20, 2008, FlexUnit will not longer be actively developed on Google Code. It is not being maintained at:

http://opensource.adobe.com/wiki/display/flexunit/

Articles / Tutorials

  • none yet

External Resources

Comment by pcaroli%...@gtempaccount.com, Nov 20, 2007

I wrote a Hello World for FlexUnit?. You can find it at: http://helloopensource.org/FlexUnit

Comment by edwardo...@gmail.com, Jan 11, 2008

Here's a link to Google Group for discussions: http://groups.google.com/group/as3flexunitlib

Comment by Mark.Jam...@gmail.com, Jan 14, 2008

Has anyone had any luck getting FlexUnit? to work with continuous integration tools (for example, CruiseControl?)?

Comment by anton.ge...@gmail.com, Feb 28, 2008

Re Continuous Integration, have you seen AIRRunner?

Comment by adam.b...@gmail.com, Mar 1, 2008

Hi Mark, We are doing this using antennae http://code.google.com/p/antennae/ We use Hudson for constant integration but this is essentially an Ant based solution and would certainly work on Cruise Control.

Comment by robco...@gmail.com, Apr 11, 2008

Hi. I wrote an utility class to simplify the configuration of a flexunit testsuite. All methods starting with "test" are added automatically as test cases when the suite is created in the following way:

private function createSuite():TestSuite? {

var ts:TestSuite? = new TestSuite?(); ts.addTest( ReflectiveTestCase?.suite(SelectItemCommandTest?)); return ts;
}

The ReflectiveTestCase? uses reflection to read all "test" methods. In this way you will never forget to add your tests to the suite!

package mypackage {

import flash.utils.describeType;
import flexunit.framework.TestSuite?;
public class ReflectiveTestCase? extends TestSuite?{
public function ReflectiveTestCase?() { }
public static function suite(clazz:Class) : TestSuite? {
var ts:TestSuite? = new TestSuite?();
var methods:XMLList = describeType(clazz).factory.method.@name; for(var i:int = 0; i <methods.length(); i++) {
var method:String = methodsi?; if(method.match("test.")) {
ts.addTest( new clazz( method ) );
}
}
return ts;
}
}
}

Comment by philip.c...@gmail.com, Apr 25, 2008

It would be nice if flexunit.framework.Assert had a assertThrows and a assertDoesNotThrow function.

Here is my implementation, which can be added to flexunit.framework.Assert (need to recompile the SWC file to use this), or just add this to a custom class that extends flexunit.framework.TestCase?, and have your test cases extend the new class:

/**
 * Test if a function call throws a certain exception when called with the given parameters. Fail if it does not.
 * 
 * @param userMessage A string containing a custom message that will be displayed along with the system message when the test fails.
 * @param errorType A class (not an instance) derived from <code>Error</code>. The type of error we are expecting not to be thrown. 
 * @param thisArg A reference to the object that will be <code>this</code> in the context of the function call (can be null).
 * @param fnct A reference to the function to be tested.
 * @param args An array of the arguments to be used forthe function call.
 */
public static function assertThrows( userMessage:String, errorType:*, thisArg:Object, fnct:Function, args:Array ):void
{
	var caughtErrType:String = "none";
	try
	{
		fnct.apply( thisArg, args );
	}
	catch(e:Error)
	{
		// if catching the requested type (or derived from), test passes
		if ( e is errorType )
			return;
		
		// caught something but not what was expected
		caughtErrType = e.name;
	}
	
	// got here if the test fails
	var failMsg:String = "expected: " + errorType.toString() + " but threw: " + caughtErrType;
	failWithUserMessage( userMessage, failMsg );			
}

/**
 * Test if a function call throws a certain exception when called with the given parameters. Fail if it does.
 * 
 * @param userMessage A string containing a custom message that will be displayed along with the system message when the test fails.
 * @param errorType A class (not an instance) derived from <code>Error</code>. The type of error we are expecting not to be thrown. 
 * @param thisArg A reference to the object that will be <code>this</code> in the context of the function call (can be null).
 * @param fnct A reference to the function to be tested.
 * @param args An array of the arguments to be used for the function call.
 */
public static function assertDoesNotThrow( userMessage:String, errorType:*, thisArg:Object, fnct:Function, args:Array ):void
{
	try
	{
		fnct.apply( thisArg, args );
	}
	catch(e:Error)
	{
		// if catching the requested type (or derived from), test fails
		if ( e is errorType )
		{
			var failMsg:String = "Threw: " + errorType;
			failWithUserMessage( userMessage, failMsg );			
		}
	}			
}

Note that if you create a custom class, you must implement failWithUserMessage, as it is private to flexunit.framework.Assert. Here is my implementation:

/**
 * Cause test to fail, format message
 * 
 * @param userMessage A string supplied by the caller of the assertion 
 * @param failMessage A string supplied by assertion test function
 */
private static function failWithUserMessage( userMessage:String, failMessage:String ):void
{
	if ( userMessage.length > 0 )
		failMessage = userMessage + " - " + failMessage;
		
	fail( failMessage );
}
Comment by rob...@gmail.com, Jun 6, 2008

Please take a look on how to test for exception in the junit project

Comment by x776...@gmail.com, Aug 2, 2008

Perhaps the "The Lost FlexUnit? Documentation", http://blog.iconara.net/2007/02/06/flexunit/, by Theo Hultberg, would be a good addition to the External Resources. In particular, Schall's example has TemperatureConverterTest?.suite(), which can be eliminated by using

ts.addTest(new TestSuite(TemperatureConverterTest));

On a related point, it seems that the ReflectiveTestCase? in the comments above produces the same result as passing a TestCase? subclass to the TestSuite? constructor, which examines the TestCase? for methods that start with 'test'.


Sign in to add a comment
Powered by Google Project Hosting