简体   繁体   中英

How to run only specific JUnit tests in Ant?

I have a sample project structure that looks like this.

在此处输入图片说明

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>

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.

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>

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 .

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>

Make use of test suites. Make different test suites with different set of test cases as per your need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM