You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Jeff Martin <je...@synamic.co.uk> on 2000/08/08 13:07:06 UTC

[PATCH] Pattern based Junit tests

This is a patch which adds a new subtask called batchtest to the junit task.
Batchtest allows you to specify a  source and a file name filter to find
test case classes. e.g. <batchtest file="**/test_*.java"
srcdir="${java.src}" /> will find all the test which start with test_ and
the execute them.

Index: src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/
junit/JUnitTask.java,v
retrieving revision 1.4
diff -r1.4 JUnitTask.java
63a64
> import org.apache.tools.ant.DirectoryScanner;
71a73,74
> import java.util.NoSuchElementException;
> import java.util.StringTokenizer;
93a97
>     private Vector batchTests = new Vector();
174a179,281
>     public JUnitBatchTest createBatchTest() {
>         final JUnitBatchTest result = new JUnitBatchTest(project);
> 
>         result.setFork(defaults.getFork());
>         result.setHaltonerror(defaults.getHaltonerror());
>         result.setHaltonfailure(defaults.getHaltonfailure());
>         result.setPrintsummary(defaults.getPrintsummary());
>         result.setPrintxml(defaults.getPrintxml());
> 
>         batchTests.addElement(result);
>         return result;
>     }
> 
>     public final class JUnitBatchTest{
>         private String[] commandLine=null;
>         private boolean fork=false;
>         private boolean haltOnError=false;
>         private boolean haltOnFailure=false;
>         private String srcDir=null;
>         private String outFile=null;
>         private boolean printSummary=false;
>         private boolean printXml=false;
>         private String fileName=null;
> 
>         private Project project=null;
> 
>         public JUnitBatchTest( Project project ){
>             this.project = project;
>         }
> 
>         public final void setCommandline(String[] args) {
>             this.commandLine = args;
>         }
>         public final void setFork(boolean value) {
>             this.fork = value;
>         }
>         public final void setHaltonerror(boolean value) {
>             this.haltOnError = value;
>         }
>         public final void setHaltonfailure(boolean value) {
>             this.haltOnFailure = value;
>         }
>         public final void setSrcdir(String value) {
>             this.srcDir = project.getBaseDir() +
System.getProperty("file.separator") + value;
>         }
>         public final void setFile(String value) {
>             this.fileName = value;
>         }
>         public final void setOutfile(String value) {
>             this.outFile = value;
>         }
>         public final void setPrintsummary(boolean value) {
>             this.printSummary = value;
>         }
>         public final void setPrintxml(boolean value) {
>             this.printXml = value;
>         }
>         public final Enumeration elements(){
>             return new FileList();
>         }
>         public class FileList implements Enumeration{
>             private String files[]=null;
>             private int i=0;
>                 
>             private FileList(){
>                 if(fileName==null)throw new BuildException("The attribute
file on batchtest must be set.");
>                 DirectoryScanner ds = new DirectoryScanner();
>                 ds.setIncludes(new String[]{fileName});
>                 ds.setBasedir(new File(srcDir));
>                 ds.scan();
>                 files = ds.getIncludedFiles();
>             }
>             public final boolean hasMoreElements(){
>                 if(i<files.length)return true;
>                 return false;
>             }
>             public final Object nextElement() throws
NoSuchElementException{
>                 if(hasMoreElements()){
>                     JUnitTest test = new JUnitTest(
>                         fork,
>                         haltOnError,
>                         haltOnFailure,
>                         printSummary,
>                         printXml,
>                         javaToClass(files[i]),
>                         outFile);
>                     i++;
>                     return test;
>                 }
>                 throw new NoSuchElementException();
>             }
>             public final String javaToClass(String fileName){
>                 StringBuffer className=new StringBuffer();
>                 StringTokenizer toke = new StringTokenizer(fileName,
System.getProperty("file.separator"));
>                 while(toke.hasMoreElements()){
>                     className.append(toke.nextToken());
>                     className.append(".");
>                 }
>                 return className.substring(0,className.length()-6);
>             }
>         }
>     }
> 
198a306,312
> 
>         // Expand the batch tests
>         for(Enumeration e = batchTests.elements();e.hasMoreElements();){
>             for(Enumeration ee =
((JUnitBatchTest)e.nextElement()).elements();ee.hasMoreElements();){
>                 tests.addElement(ee.nextElement());
>             }
>         }

Re: [PATCH] Pattern based Junit tests

Posted by Stefan Bodewig <bo...@bost.de>.
>>>>> "JM" == Jeff Martin <je...@synamic.co.uk> writes:

 JM> This is a patch which adds a new subtask called batchtest to the
 JM> junit task.

I'm in the course of rewriting most of the JUnit stuff at the moment,
I'm going to include the functionality but will base it on the
include/exclude mechanism like the one for filesets. I.e.

<batchtest dir="${java.src}>
  <include name="**/test_*.java" />
</batchtest>

is this OK?

Other things I'm going to change:

* add if and unless attributes to test (and possibly batchtest?). Should
behave like the ones for targets and include/exclude.

* remove the string "default" from the attributes of JUnit.

* remove printxml everywhere (see <formatter> below). Remove
printsummary from <test>.

* remove defaultoutfile. All this does is hand a default filename to
the XML Formatter - which we should do anyway if the user want XML
output and doesn't specify a filename.

* add a nested <formatter> tag, both to <junit> as well as to <test>
(and <batchtest>?).

<junit>
  <formatter type="xml">
</junit>

will do the same as defaultprintxml did.

<junit>
  <formatter type="plain">
</junit>

will produce plain text files with the same content the XML files had.

<junit>
  <formatter classname="my.own.Formatter" />
</junit>

will use my.own.Formatter, which must implement
org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter.

What do you think of these ideas? Did I remove something that will
break your setup - and doesn't provide a different way to achieve the
same?

Stefan