ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Joel Hockey" <joelhoc...@hotmail.com>
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 java.io.File;

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 = 
Logger.getLogger(DynamicTestSuite.class);

    /** 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");

        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 {
                    suite.addTestSuite(Class.forName(className));
                } 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">
      <classpath>
        <path location="build/classes"/>
        <path location="build/test-classes"/>
        <path refid="classpath"/>
      </classpath>
      <formatter type="xml"/>
      <formatter type="plain" usefile="false"/>
      <sysproperty key="test.classes.dir" 
value="${basedir}/build/test-classes"/>
      <sysproperty key="test.suffix" value="${test.pattern}Test.class"/>
      <test name="cash.DynamicTestSuite" todir="build/report"/>
    </junit>

My build.properties contains:

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

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  
http://ninemsn.com.au/premium/landing.asp


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message