As the first step, let's create a sub directory called “mytest” and put the bellow test class to the directory. For simplicity this test class doesn't really test anything.
import junit.framework.TestCase;
public class HelloWorld extends TestCase {
public HelloWorld(String text){
super (text);
}
public void testprint1(){
System.out.println("Hello World1");
assertTrue( "TestExample", true );
}
public void testprint2(){
System.out.println("Hello World2");
assertTrue( "TestExample", true );
}
}
Then create the below test suite in the same subdirectory. In JUnit, "suites" can be used to run a group of test classes and allows for some additional functionality.
import junit.framework.Test;
import junit.framework.TestSuite;
public class HelloworldSuite extends TestSuite {
public static Test suite(){
TestSuite suite = new TestSuite();
suite.addTest(new HelloWorld("testprint1"));
suite.addTest(new HelloWorld("testprint2"));
suite.addTest(new HelloWorld("testprint1"));
return suite;
}
}
Now create a directory called “lib” inside your “mytest” directory and copy the junit.jar file to it. In my case, I'm using junit-4.4.jar. In addition to that you need to add junit.jar to your CLASSPATH or copy junit.jar to your Ant library(ANT_HOME/lib) from your JUnit library to enable the integration of JUnit and Ant.
Now put the below build.xml file to your “mytest” directory.
<project name="Hello-world-sample" basedir="." default="test">
<property name="dest.dir" value="build" />
<property name="dest.dir.classes" value="${dest.dir}/classes" />
<property name="dest.dir.lib" value="${dest.dir}/lib" />
<property name="home" value="." />
<path id="build.class.path">
<fileset dir="${home}/lib">
<include name="*.jar" />
</fileset>
</path>
<path id="test.class.path">
<pathelement location="${dest.dir.classes}" />
</path>
<target name="clean">
<delete dir="${dest.dir}" />
</target>
<target name="prepare">
<mkdir dir="${dest.dir}" />
<mkdir dir="${dest.dir.classes}" />
</target>
<target name="compile" depends="clean,prepare">
<javac srcdir="src" destdir="${dest.dir.classes}">
<classpath refid="build.class.path" />
</javac>
</target>
<target name="run" depends="compile"/>
<target name="test" depends="compile">
<junit>
<classpath refid="test.class.path" />
<classpath refid="build.class.path" />
<formatter type="brief" usefile="false" />
<test name="HelloworldSuite" />
</junit>
</target>
</project>
C:\Ant_Test>ant test
Buildfile: build.xml
clean:
[delete] Deleting directory C:\Ant_Test\build
prepare:
[mkdir] Created dir: C:\Ant_Test\build
[mkdir] Created dir: C:\Ant_Test\build\classes
compile:
[javac] Compiling 4 source files to C:\Ant_Test\build\classes
test:
[junit] Testsuite: HelloworldSuite
[junit] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 0 sec
[junit]
[junit] ------------- Standard Output ---------------
[junit] inside test print1
[junit] inside test print2
[junit] inside test print3
[junit] ------------- ---------------- ---------------
BUILD SUCCESSFUL
Total time: 1 second
Note that if you use packages then the test name in the build.xml should be change as
<test name="package.name/HelloworldSuite"/>
6 comments:
Hi... I'm not sure your code is up to date. I haven't seen anyone building suite-classes the last 5+ years..
to get code coverage using ant see
http://firstclassthoughts.co.uk/ant/code_coverage.html
Hi zak,
Pls follow this guide and say if you have any problem. Why you are saying no onee building suite-classes, I have used it for my test automations. And I know lots of people are land to this little tutorial to see runnig junit test suite from ant.
Krishantha.
It is a better way. All methods of test cases was runed
public class SaleTestSuite extends TestSuite {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new TestSuite(KeyManagerTestCase.class));
suite.addTest(new TestSuite(SaleCoreFacadeTestCase.class));
suite.addTest(new TestSuite(CardServiceTest.class));
suite.addTest(new TestSuite(CategoryServiceTestCase.class));
suite.addTest(new TestSuite(ConfigurationServiceTest.class));
suite.addTest(new TestSuite(CurrencyRateServiceTest.class));
suite.addTest(new TestSuite(GoodsAttributeServiceTest.class));
suite.addTest(new TestSuite(GoodsAttributeValueServiceTest.class));
suite.addTest(new TestSuite(GoodsServiceTestCase.class));
suite.addTest(new TestSuite(ItemServiceTest.class));
suite.addTest(new TestSuite(OrderServiceTest.class));
suite.addTest(new TestSuite(PriceListTemplateServiceTestCase.class));
suite.addTest(new TestSuite(SubpaymentServiceTestCase.class));
suite.addTest(new TestSuite(VolumeServiceTestCase.class));
suite.addTest(new TestSuite(SaleSoapServiceTest.class));
return suite;
}
}
test:
[junit] Testsuite: HelloworldSuite
[junit] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 0 sec
[junit]
[junit] ------------- Standard Output ---------------
[junit] Hello World1
[junit] Hello World2
[junit] Hello World3
[junit] -------------
Why is mine different from yours? Could it be because of the Software versions(Ant and Junit)...
Thanks so much fo sharing...
Hi krishnakanth,
Thanks for sharing the code and i could use it successfully .But some of my tests are actually dbunit tests and they have xmls and builsscript is not able to find those xmls .Can you suggest me a way out?
Excellent tutorial. some of your readers may also want to download the source code shown above as a Java application zip
Post a Comment