What's new? | Help | Directory | Sign in
Google
as3flexunitlib
ActionScript 3.0 framework for unit testing.
  
  
  
  
    
Search
for
Updated Jan 15, 2008 by mikechambers
Labels: Featured
Resources  

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

Articles / Tutorials

External Resources


Comment by pcar...@thoughtworks.com, Nov 20, 2007

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

Comment by louisbarman, Nov 27, 2007
Comment by edwardotis, Jan 11, 2008

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

Comment by Mark.James.Fletcher, Jan 14, 2008

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

Comment by anton.georgiev, Feb 28, 2008

Re Continuous Integration, have you seen AIRRunner?

Comment by adam.boas, Mar 01, 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 robcos78, 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 phil...@cae.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 robcos, Jun 06, 2008

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


Sign in to add a comment