简体   繁体   中英

Ant and annotated junit tests

I have the following test class in ./src/com.example.package.tests

public class CardTest {
    @Test
    public void testCardCompareWithSameValue() throws Exception {
        Card card1 = new Card(Card.COLOR.CLUBS, 4);
        Card card2 = new Card(Card.COLOR.SPADES, 4);

        Card.MATCH_TYPE match = card1.compareCard(card2);

        assertEquals("The returned match must be of enum type Card.MATCH_TYPE.VALUE",
            Card.MATCH_TYPE.VALUE, match);
    }
}

I have placed junit-4.7.jar in ./lib

and have the following ./build.xml

<project name="Project" basedir="." default="main">

    <property name="src.dir" value="src" />
    <property name="build.dir" value="build" />
    <property name="classes.dir" value="${build.dir}/classes" />
    <property name="jar.dir" value="${build.dir}/jar" />
    <property name="lib.dir" value="lib" />
    <property name="main-class" value="com.example.package.gui.Main" />
    <property name="report.dir" value="${build.dir}/junitreport" />

    <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar" />
    </path>


    <path id="application" location="${jar.dir}/${ant.project.name}.jar"/>


    <target name="clean">
        <delete dir="${build.dir}" />
    </target>

    <target name="compile">
        <mkdir dir="${classes.dir}" />
        <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" />
        <copy todir="${classes.dir}">
            <fileset dir="${src.dir}" excludes="**/*.java" />
        </copy>
    </target>

    <target name="jar" depends="junit">
        <mkdir dir="${jar.dir}" />
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}" excludes="**/*Test.class">
            <manifest>
                <attribute name="Main-Class" value="${main-class}" />
            </manifest>
        </jar>

    </target>


    <target name="junit" depends="compile">
        <mkdir dir="${report.dir}" />
        <junit printsummary="yes" haltonfailure="yes" showoutput="yes">
            <classpath>
                <path refid="classpath" />
                <path location="${classes.dir}" />
                <path refid="application" />
            </classpath>

            <formatter type="xml" />

            <batchtest fork="yes">
                <fileset dir="${src.dir}" includes="*Test.java" />
            </batchtest>
        </junit>
    </target>

    <target name="junitreport" depends="junit">
        <junitreport todir="${report.dir}">
            <fileset dir="${report.dir}" includes="TEST-*.xml" />
            <report todir="${report.dir}" />
        </junitreport>
    </target>


    <target name="run" depends="jar">
        <java classname="${main-class}" fork="true" >
            <classpath>
                <path refid="classpath" />
                <path refid="application" />
            </classpath>
        </java>
    </target>

    <target name="clean-build" depends="clean,jar" />

    <target name="main" depends="clean,run" />

</project>

Why does ant not run my junit tests? When I check the report it says that there were 0 tests.

I am using OS X Yosemite and installed Ant via the binary download.

Edit: Tested with ubuntu as well, no change.

When you run <junit> , you want to refer to the compiled test class files and not the test Java source files .

I recommend to put your regular sources and test sources into two different directories, then compile them into two different directories. We use the Maven directory standards:

  • src/main/java - Location of the regular source files.
  • src/main/resources - Location of the resources (XML and JSON files that go into the jar).
  • src/test/java - Location of the test source files.
  • src/test/resources - Location of the resources needed for testing.
  • target/classes - Where regular source class files are compiled.
  • target/test-classes - Where test class files are compiled.

Example:

<target name="compile">
    <javac srcdir="src/main/java"
        destdir="target/classes">
        <classpath ref="main.classpath"/>
    </javac>
</target>

<target name="package"
    depends="compile">
    <jar destfile="target/${ant.project.name}.jar">
        <fileset dir="target/classes"/>
        <fileset dir="src/main/resources"/>
    </jar>
</target>

<target name="compile-test"
    depends="compile">

    <!-- Classpath must include the main.classpath from -->
    <!-- above, the test.classpath which will include   -->
    <!-- "junit.jar", and finally the compiled classes  -->
    <!-- from target/classes. Not 100% sure if this is  -->
    <!-- the correct way to specify it, but it's close. -->
    <javac srcdir="src/test/java"
        destdir="target/test-classes">
        <classpath>
             <path ref="main.classpath"/>
             <path ref="test.classpath"/>
             <path location="target/classes"/>
        <classpath>
    </javac>
</target>

<junit printsummary="yes"
    haltonfailure="yes"
    showoutput="yes">
    <classpath>
        <path refid="main.classpath" />
        <path refid="test.classpath" />
        <path location="${classes.dir}" />
    </classpath>

        <!-- Batchtest is pointing to the classes-->
        <formatter type="xml" />
        <batchtest fork="yes">
            <fileset dir="${target/test-classes}"/>
        </batchtest>
    </junit>
</target>

According to the Ant JUnit Task documentation you have to tell the batchtest where to put the test reports using the todir attribute. Otherwise it will write them to the current working directory.

<batchtest fork="yes"  todir="${report.dir}">
            <fileset dir="${src.dir}" includes="*Test.java" />
        </batchtest>

Should work.

Also check the current working directory to see if you have a bunch of TEST-*.xml files

This was what worked, a combination of @dawid-w 's and @dkatzel 's answers.

        <batchtest fork="yes" skipnontests="yes" todir="${report.dir}">
            <fileset dir="${classes.dir}" />
        </batchtest>

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