繁体   English   中英

如何从命令行使用junit运行多个测试类?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM