[英]How to run multiple test classes with junit from command line?
我知道要從命令行運行junit,您可以執行以下操作:
java org.junit.runner.JUnitCore TestClass1 [...其他測試類...]
但是,我想一起運行許多測試,並且手動鍵入“ TestClass1 TestClass2 TestClass3 ...”只是效率低下。
當前,我將所有測試類組織在一個目錄中(該目錄具有指示軟件包的子目錄)。 有什么方法可以從命令行運行junit並讓它一次執行所有這些測試類?
謝謝。
基本上有兩種方法可以執行此操作:使用Shell腳本收集名稱,或者使用ClassPathSuite
在Java類路徑中搜索與給定模式匹配的所有類。
類路徑套件方法對於Java更自然。 這樣的答案描述了如何最好地使用ClassPathSuite。
shell腳本方法有點笨拙且特定於平台,並且可能會因測試次數而遇到麻煩,但是如果您出於任何原因試圖避免ClassPathSuite的話,它將可以解決問題。 這個簡單的例子假設每個測試文件都以“ Test.java”結尾。
#!/bin/bash
cd your_test_directory_here
find . -name "\*Test.java" \
| sed -e "s/\.java//" -e "s/\//./g" \
| xargs java org.junit.runner.JUnitCore
我發現可以編寫一個Ant構建文件來實現此目的。 這是示例build.xml:
<target name="test" description="Execute unit tests">
<junit printsummary="true" failureproperty="junit.failure">
<classpath refid="test.classpath"/>
<!-- If test.entry is defined, run a single test, otherwise run all valid tests -->
<test name="${test.entry}" todir="${test.reports}" if="test.entry"/>
<batchtest todir="tmp/rawtestoutput" unless="test.entry">
<fileset dir="${test.home}">
<include name="**/*Test.java"/>
<exclude name="**/*AbstractTest.java"/>
</fileset>
<formatter type="xml"/>
</batchtest>
<fail if="junit.failure" message="There were test failures."/>
</target>
使用此構建文件,如果要執行單個測試,請運行:
ant -Dtest.entry=YourTestName
如果要批量執行多個測試,請在上例中所示的<batchtest>...</batchtest>
下指定相應的測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.