2012-09-02 101 views
6

我正在使用纯JUnit 4测试,并将它们编译到某些属于类路径的jar中。 我想在我的类路径中运行所有测试。JUnit在JAR中运行测试

有没有办法?

我的任务是这样的:

<target name="test" description="All the tests"> 
    <junit fork="yes"> 
     <classpath> 
      <path refid="test.classpath" /> 
      <fileset dir="war/WEB-INF/lib/"> 
       <include name="*.jar"/> 
      </fileset> 
      <fileset dir="war/WEB-INF"/> 
      <fileset dir="war"/> 
     </classpath> 
    <formatter type="plain" usefile="false" /> 

    </junit> 
</target> 

回答

5

下面的例子假设JUnit测试的命名后缀为“测试”,如“MyClassTest.java”,而JUnit 4中的jar文件是在图书馆目录由物业lib.dir指出。对于包含编译测试的每个jar文件,可以使用ZipFileSet来选择嵌套的<batchtest>元素内的测试类。 JUnit 4 jar的路径包含在类路径中,以确保使用正确的版本。

<target name="test" description="All the tests."> 
    <junit fork="true"> 
    <classpath> 
     <fileset dir="war/WEB-INF/lib/" includes="**/*.jar" /> 
     <fileset dir="${lib.dir}" includes="junit*.jar" /> 
    </classpath> 
    <batchtest haltonfailure="true"> 
     <zipfileset src="war/WEB-INF/lib/test.jar" includes="**/*Test.class" /> 
    </batchtest> 
    <formatter type="plain" usefile="false" /> 
    </junit> 
</target> 
+0

它只是工作,谢谢 –