[英]How to run only specific JUnit tests in Ant?
我有一个看起来像这样的示例项目结构。
现在我只想通过build.xml而不是MyTest运行MyTestTwo。 我该怎么做呢? 当我尝试仅运行一个测试时,我通过执行以下操作实现了这一目标:
<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>
利用测试套件。 根据需要使用不同的测试用例集制作不同的测试套件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.