我在./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);
}
}
我已将junit-4.7.jar放在./lib
中并拥有以下./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>
为什么ant没有运行我的junit测试?当我查看报告时,它说有0个测试。
我正在使用OS X Yosemite并通过二进制下载安装Ant。
编辑:同样用ubuntu测试,没有变化。
答案 0 :(得分:3)
当您运行<junit>
时,您想要参考已编译的测试 类文件 ,而不是测试Java 源文件< / EM> 强>
我建议将常规源和测试源放到两个不同的目录中,然后将它们编译成两个不同的目录。我们使用Maven目录标准:
src/main/java
- 常规源文件的位置。src/main/resources
- 资源的位置(进入jar的XML和JSON文件)。src/test/java
- 测试源文件的位置。src/test/resources
- 测试所需资源的位置。target/classes
- 编译常规源类文件。target/test-classes
- 编译测试类文件的位置。示例:
<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>
答案 1 :(得分:1)
根据Ant JUnit Task documentation,您必须告诉batchtest
使用todir
属性放置测试报告的位置。否则,它会将它们写入当前工作目录。
<batchtest fork="yes" todir="${report.dir}">
<fileset dir="${src.dir}" includes="*Test.java" />
</batchtest>
应该工作。
还要检查当前的工作目录,看看你是否有一堆TEST - * .xml文件
答案 2 :(得分:0)
这是有用的,是@ dawid-w和@dkatzel的答案的组合。
<batchtest fork="yes" skipnontests="yes" todir="${report.dir}">
<fileset dir="${classes.dir}" />
</batchtest>