How to run all JUnit tests in a category/suite with Ant?

前端 未结 2 1089
孤街浪徒
孤街浪徒 2020-12-01 07:58

I\'m using JUnit Categories and ClassPathSuite in a setup similar to that described in this answer. To recap:

public interface FastTests {
}

@RunWith(Catego         


        
相关标签:
2条回答
  • 2020-12-01 08:47

    I found a workaround to use ant's junit task to run test of a specific Category:

    <target name="test" description="Run Selenium tests by category">
        <fail unless="category.name" message="Please specify 'category.name' property"/>
    
        <junit showoutput="true" printsummary="true" fork="true">
            <formatter type="xml"/>
            <classpath refid="test.classpath"/>
    
            <batchtest todir="${test.reports}" haltonfailure="false">
                <fileset dir="${classes}">
                    <!-- regex '^\s*@Category' part added to ensure @Category annotation is not commented out -->
                    <containsregexp expression="^\s*@Category.*${category.name}"/>
                </fileset>
            </batchtest>
        </junit>
    </target>
    

    Execute test by supplying category.name property to ant like this:

    ant -Dcategory.name=FastTests test
    

    Using batchtest will also produce separate JUnit XML report files per test (e.g. TEST-fi.foobar.FastTestClassN.xml).

    0 讨论(0)
  • 2020-12-01 08:48

    Right, I got it working with <batchtest> quite simply:

    <junit showoutput="true" printsummary="yes" fork="yes">
        <formatter type="xml"/>
        <classpath refid="test.classpath"/>
        <batchtest todir="${test.reports}">
            <fileset dir="${classes}">
                <include name="**/FastTestSuite.class"/>
            </fileset>
        </batchtest>
    </junit>
    

    I had tried <batchtest> earlier, but had made the silly mistake of using "**/FastTestSuite.java" instead of "**/FastTestSuite.class" in the <include> element... Sorry about that :-)

    NB: it's necessary to set fork="yes" (i.e., run the tests in a separate VM); otherwise this will also produce "initializationError" at java.lang.reflect.Constructor.newInstance(Constructor.java:513) like <test> (see comments on the question). However, I couldn't get <test> working even with fork="yes".

    The only shortcoming is that this produces just one JUnit XML report file (TEST-fi.foobar.FastTestSuite.xml) which makes it look like all the (hundreds) of tests are in one class (FastTestSuite). If anyone knows how to tweak this to show the tests in their original classes & packages, please let me know.

    0 讨论(0)
提交回复
热议问题