2012-09-02 102 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

它只是工作,謝謝 –