2015-09-14 119 views
0

我有一個看起來像這樣的示例項目結構。如何在Ant中只運行特定的JUnit測試?

​​

現在我只想通過build.xml文件,而不是MyTest的運行MyTestTwo。我如何去做這件事?當我試圖只運行一個測試,然後我得到它做這個:

<target name="runJUnit" depends="compile"> 
    <junit printsummary="on"> 
     <test name="com.edu.BaseTest.MyTest"/>   
     <classpath> 
      <pathelement location="${build}"/> 
      <pathelement location="Path to junit-4.10.jar" /> 
     </classpath> 
    </junit> 
</target> 

如何做到這一點,如果我必須要麼爲上述項目結構或者是什麼,如果有10次不同的測試,我只想做他們中的5人跑步?我是Ant新手,所以任何幫助將不勝感激。謝謝。

回答

1

Juned Ahsan建議使用測試套件的答案很好。但是,如果問題意味着您正在尋找一個完全包含在您的ant文件中的解決方案,那麼您可以使用batchtest元素來指定要使用螞蟻文件集運行的測試。

<!-- Add this property to specify the location of your tests. --> 
<property name="source.test.dir" location="path_to_your_junit_src_dir" /> 
<!-- Add this property to specify the directory in which you want your test report. --> 
<property name="output.test.dir" location="path_to_your_junit_output_dir" /> 

<target name="runJUnit" depends="compile"> 
    <junit printsummary="on"> 
     <test name="com.edu.BaseTest.MyTest"/>   
     <classpath> 
      <pathelement location="${build}"/> 
      <pathelement location="Path to junit-4.10.jar" /> 
     </classpath> 

     <batchtest fork="yes" todir="${output.test.dir}"> 
      <!-- The fileset element specifies which tests to run. --> 
      <!-- There are many different ways to specify filesets, this 
       is just one example. --> 
      <fileset dir="${source.test.dir}" includes="**/MyTestTwo.java"/> 
     </batchtest> 
    </junit> 
</target> 

正如上面的代碼註釋表明,有使用的文件集來指定哪些文件包括和排除許多不同的方式。您選擇哪種形式即可使用。這實際上取決於你希望如何管理你的項目。有關文件集的更多信息,請參閱:https://ant.apache.org/manual/Types/fileset.html

請注意,文件集中的「** /」是匹配任何目錄路徑的通配符。因此MyTestTwo.java會不管它是在什麼目錄匹配

的其他可能的文件集的規格,你可以使用:

<fileset dir="${source.test.dir}"> 
    <include name="**/MyTestTwo.java"/> 
    <exclude name="**/MyTest.java"/> 
</fileset> 
0

利用測試套件。根據您的需要,針對不同的測試用例組製作不同的測試套件。

+0

你的意思是創建一個'TestSuite'類和相關的'TestRunner'類然後使用'javac'編譯''build.xml'中的'java'來運行'TestRunner'類? – user3044240

相關問題