ASUnit Templates for Eclipse

Jun 13 2005 Published by under Flash

OK, so I’ve been using Eclipse more and more. One thing I missed about it while using ASUnit is the missing Create Classes command that you can run from the Flash IDE. This recursively creates AllTests classes in each directory of your package, down to the class you are writing a test for, and creates a TestCase class for the specificy class. Using FAME, you have to write all of these yourself. I did this once, and that was enough. Then I created templates to do it for me after this:

AllTests:

import com.asunit.framework.*;

class AllTests extends TestSuite {
	private var className:String = "AllTests";

	public function AllTests()
	{
		super();
		addTest(new ${package}.AllTests());
		// and/or add specific tests here
	}
}

TestCase:

import ${package}.*;
import com.asunit.framework.*;

class ${package}.${className} extends TestCase {
	private var className:String = "${package}.${className}";
	private var instance:${className};

	public function setUp():Void {
		instance = new ${className}();
	}

	public function tearDown():Void {
		delete instance;
 	}

 	public function testInstantiated():Void {
		assertTrue("${className} instantiated", instance instanceof ${className});
	}
}

Just go to Windows/Preferences/ActionScript 2/Templates. Click “New…”, add name and description and paste the code.

Now, in an .as file, type “TestCase” or “AllTests” and hit control-space. You’ll be prompted for the needed info to finish the class.

7 responses so far. Comments will be closed after post is one year old.

  • darron says:

    I’ll add these to the default templates that ship with ASDT. 🙂 Sure, it shows biased towards AsUnit (over the others), but AsUnit seems to be the most common.

  • Keith Peters says:

    Nice! they are my very first Eclipse templates, so feel free to tweak them if they aren’t quite right.

  • wow Keith! I didn’t know about templates in Eclipse, really useful stuff. Thans for sharing.

  • Hey Keith,

    are ${className} and ${package} standard template variables? All I can find is ${enclosing_package} and so on.

    This means that I need to place my classes under the project root and not in e.g. source/classes/com/company/… or my package will look like source.classes.com.company…. when I use the template in a file.

  • Keith Peters says:

    classname and package are just variables i made up, just to tell you what to put there. You have to manually type it in, but you just do it once and it replaces it througout the file.

  • Hey Keith,

    it personally think that it is better to use the variables eclipse provides. It can save you a lot of time.

    Try ${enclosing_type} instead of className for instance. You can find all vars on the bottom left of the template edit screen.

  • Keith Peters says:

    thanks Christophe. just updated them.