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

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

问题描述

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

I have a sample project structure that looks like this.

现在我只想通过build.xml而不是MyTest运行MyTestTwo.我该怎么做呢?当我尝试仅运行一个测试时,便通过以下方式实现了该目标:

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/fileset.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天全站免登陆