Monday, 2 June 2008

Easier Flexunit configuration

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:
[source:js]
private function createSuite():TestSuite {
var ts:TestSuite = new TestSuite();
ts.addTest( ReflectiveTestCase.suite(SelectItemCommandTest));
return ts;
}
[/source]
The ReflectiveTestCase uses reflection to read all "test" methods. In this way you will never forget to add your tests to the suite!

[source:js]
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 var method:String = methods[i];
if(method.match("test.*")) {
ts.addTest( new clazz( method ) );
}
}
return ts;
}
}
}
[/source]

No comments:

Post a Comment