[英]Ant and annotated junit tests
我在./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一起测试,没有变化。
运行<junit>
,您要引用已编译的测试类文件,而不是测试Java 源文件 。
我建议将常规源和测试源放在两个不同的目录中,然后将它们编译到两个不同的目录中。 我们使用Maven目录标准:
src/main/java
常规源文件的位置。 src/main/resources
- (即进入罐子XML和JSON文件)的资源的位置。 src/test/java
测试源文件的位置。 src/test/resources
测试所需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>
根据Ant JUnit Task文档,您必须使用todir
属性告诉batchtest
将测试报告放在何处。 否则,会将它们写入当前工作目录。
<batchtest fork="yes" todir="${report.dir}">
<fileset dir="${src.dir}" includes="*Test.java" />
</batchtest>
应该管用。
还要检查当前的工作目录,以查看是否有一堆TEST-*。xml文件
这是有效的方法,将@ dawid-w和@dkatzel的答案结合在一起。
<batchtest fork="yes" skipnontests="yes" todir="${report.dir}">
<fileset dir="${classes.dir}" />
</batchtest>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.