ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Joel Hockey" <>
Subject RE: junit batchtest using a single testsuite
Date Tue, 10 Feb 2004 23:33:20 GMT
I have come up with a solution for my problem.  I have created a 
DynamicTestSuite that creates a test suite by matching all classes in a 
given classpath against a given suffix.  (This could easily be changed to 
use a regular expression).

My class is (package cash is named after Johnny Cash):

package cash;


import junit.framework.Test;
import junit.framework.TestSuite;

import org.apache.log4j.Logger;

* Dynamically search for all test classes and construct a single TestSuite
* @author Joel Hockey
* @version $Id: $
public class DynamicTestSuite {
    /** System property containing full path to test classes dir - 
<code>test.classes.dir</code> */
    public static final String TEST_CLASSES_DIR = "test.classes.dir";
     * Suffix of class files to include in suite - <code>test.suffix</code>.
     * <code>Test.class</code> is a good choice.
    public static final String TEST_SUFFIX = "test.suffix";

    private static final Logger LOG = 

    /** private default constructor */
    private DynamicTestSuite() { }

    /** return suite */
    public static Test suite() {
        String testSuffix = System.getProperty(TEST_SUFFIX);
        LOG.debug("Adding classes that end with [" + testSuffix + "] to 

        TestSuite suite = new TestSuite();

        File root = new File(System.getProperty(TEST_CLASSES_DIR));
        int startPackage = root.getAbsolutePath().length() + 1;
        addDirectory(root, suite, startPackage, testSuffix);
        return suite;

    /** Recursively adds all files that match to the given suite,  starting 
at given directory */
    private static void addDirectory(File dir, TestSuite suite, int 
startPackage, String testSuffix) {
        String[] paths = dir.list();
        for (int i = 0; i < paths.length; i++) {
            File f = new File(dir, paths[i]);
            if (f.isFile() && f.getName().endsWith(testSuffix)) {
                String absPath = f.getAbsolutePath();
                String className = absPath.substring(startPackage, 
absPath.length() - 6);
                className = className.replace(File.separatorChar, '.');
                LOG.debug("Adding class to TestSuite: " + className);
                try {
                } catch (ClassNotFoundException e) {
                    LOG.error("Could not load class into TestSuite: " + 
className, e);
            } else if (f.isDirectory()) {
                addDirectory(f, suite, startPackage, testSuffix);

My ant junit task is:

    <junit printSummary="true" fork="yes" haltonfailure="yes">
        <path location="build/classes"/>
        <path location="build/test-classes"/>
        <path refid="classpath"/>
      <formatter type="xml"/>
      <formatter type="plain" usefile="false"/>
      <sysproperty key="test.classes.dir" 
      <sysproperty key="test.suffix" value="${test.pattern}Test.class"/>
      <test name="cash.DynamicTestSuite" todir="build/report"/>

My contains:

# use test.pattern to test only certain classes.  Give Class name.

This allows me to run all tests, run a single test, or run any tests that 
have the same suffix.

Get less junk mail with ninemsn Premium. Click here

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message