我在 ./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
- 资源(进入 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>
关于java - Ant 和带注释的 junit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28235691/