ASUnit Templates for Eclipse

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.

This entry was posted in Flash. Bookmark the permalink.

7 Responses to ASUnit Templates for Eclipse

  1. 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.

  2. Keith Peters says:

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

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

  4. 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.

  5. 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.

  6. 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.

  7. Keith Peters says:

    thanks Christophe. just updated them.

Comments are closed.