问题描述
我知道从命令行运行junit你可以这样做:
I know that to run junit from command line you can do this:
java org.junit.runner.JUnitCore TestClass1 [... other test classes .. 。]
java org.junit.runner.JUnitCore TestClass1 [...other test classes...]
但是,我想一起运行多个测试,并手动键入TestClass1 TestClass2 TestClass3 ...效率低下。
However, I want to run many tests together and manually typing "TestClass1 TestClass2 TestClass3..." is just inefficient.
当前我在一个目录(其中有子目录指示包)中组织所有测试类。有什么方法,我可以从命令行运行junit,让它一次执行这些测试类。
Current I organize all test classes in a directory (which has sub-directories indicating the packages). Is there any way that I can run junit from command line and let it execute these test classes all at once?
谢谢。
推荐答案
我发现我可以编写一个Ant构建文件来实现这一点。这里是样例build.xml:
I found that I can write an Ant buildfile to achieve this. Here is the sample 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>
b
$ b
With this build file, if you want to execute a single test, run:
ant -Dtest.entry=YourTestName
如果要批量执行多个测试,请指定< batchtest> ...< / batchtest>
如上例所示。
If you want to execute multiple tests in batch, specify the corresponding tests under the <batchtest>...</batchtest>
as shown in the above example.
这篇关于如何从命令行使用junit运行多个测试类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!