Tuesday, June 30, 2009

Automate Unit Test Execution

A lot of times, when doing TDD, we make small refactorings and then have to manually go ahead and run the tests to figure out whether the changes we made are consistent and have not broken the tests. Wouldn't it be good if the tests were run on each Save to ascertain that we have not broken any of the tests. This also lets us know which changes are the culprit. Using eclipse we can automate running the unit tests on each change and save we make. Following are the steps needed in order to accomplish this.
  1. Create a main class
import junit.framework.TestSuite;
import junit.textui.TestRunner;
public class TestBootstrap
{
public static void main(String[] args)
{
TestSuite testSuite=new TestSuite();
testSuite.addTestSuite(TestClass.class);
TestRunner.run(testSuite);
}
}

2. Create an ant build file

<project>
<target name="eclipse-test-runner">
<java classname="TestBootstrap" classpath="bin:libs/junit.jar"> <java> </target>
</project>

3. Create a Builder in eclipse
Open project properties: Right click project -> Propertie. Create new ant builder step after the Java Builder in eclipse: Builders -> new -> Ant Builder. Give your builder a nice name.
Main: Select the build file and the base directory
Targets: Select your ANT target for: “After Clean”, “Manual Build”, and “Auto Build” (most important) Build Options: Check “Specify working set” and select all of your source folders.
And thats all, now every time you save a file, eclipse will run the ant target specified in your build file, which incidentally calls your TestBootstrap's main and invoked all the tests.



The emperor and me beaching

The Devil next door

Kaiser The Emperor