且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-19 23:39:46

来自 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>