如何在 Ant 中仅运行特定的 JUnit 测试? [英] How to run only specific JUnit tests in Ant?

查看:31
本文介绍了如何在 Ant 中仅运行特定的 JUnit 测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的示例项目结构.

I have a sample project structure that looks like this.

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

Now I want to only run MyTestTwo through build.xml and not MyTest. How do I go about doing this? When I tried running only one test then I achieved it by doing this:

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

How do I achieve this if I have to do either for the above project structure or what if there were 10 different tests and I only want 5 of them to run? I am new to Ant, so any help will be appreciated. Thank You.

推荐答案

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

The answer from Juned Ahsan, which recommends using test suites is a good one. If however, as the question implies, you are looking for a solution that is entirely contained within your ant file, then you can make use of the batchtest element that specifies tests to be run using a ant fileset.

<!-- 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/文件集.html.

As the code comment above indicates, there are many different ways to use filesets to specify which files to include and exclude. Which form you chose is up to use. It really depends on how you wish to manage your project. For more info on filesets see: https://ant.apache.org/manual/Types/fileset.html.

请注意,文件集中的**/"是匹配任何目录路径的通配符.因此无论 MyTestTwo.java 位于哪个目录,都会匹配.

Note that the "**/" in the fileset is a wildcard that match any directory path. Thus MyTestTwo.java would be matched regardless of what directory it is in.

您可以使用的其他可能的文件集规范:

An other possible fileset specifications you could use:

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

这篇关于如何在 Ant 中仅运行特定的 JUnit 测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆