java - 如何在 Ant 中仅运行特定的 JUnit 测试?

标签 java ant junit

我有一个如下所示的示例项目结构。

enter image description here

现在我只想通过 build.xml 运行 MyTestTwo 而不是 MyTest。我该怎么做呢?当我尝试只运行一个测试时,我通过这样做实现了它:

<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 的新手,所以任何帮助将不胜感激。谢谢。

最佳答案

Juned Ahsan 的答案是一个很好的答案,建议使用测试套件。但是,如果正如问题所暗示的那样,您正在寻找完全包含在 ant 文件中的解决方案,那么您可以使用 batchtest 元素来指定要使用 ant 运行的测试>文件集

<!-- 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>

关于java - 如何在 Ant 中仅运行特定的 JUnit 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32556060/

相关文章:

java - Ant 根本无法识别我的jar文件

java - 如何使用 jsoup 从这个 html 页面获取文本?

java - 如何释放 Activity 完成的线程?

java - Netbeans: "Run -> Test Project"不执行任何操作

java - Ant loadproperties 失败(bcel 错误?)

java - 使用 Thread Local 的应用程序的单元测试部分

java - 设置 Grails 分布式缓存时 ehcacheMulticastGroupAddress 出错

java - Log4j2 RollingFileAppender 构建器方法返回类型错误

java - 用Java和JUnit实现球体体积公式?

java - 模拟 KeyStore 以测试 KeyStoreException