这是我用来运行测试的目标:
<target name="run-tests" description="run the tests" depends="compilation">
<junit>
<sysproperty key="tests.basedir" value="${SPECIAL_PATH}/unit_tests"/>
<classpath>
<pathelement location="${COMPILED_CLASSES}"/>
<pathelement location="${basedir}/junit-4.8.1.jar"/>
</classpath>
<batchtest>
<fileset dir="${COMPILED_CLASSES}/unit_tests/">
<include name="**/Test*.class"/>
<exclude name="**/*$*"/>
</fileset>
</batchtest>
</junit>
</target>
但是,每次我尝试运行这个目标时,我的所有测试都会失败,例如:
[junit] java.lang.ClassNotFoundException: testpackage.TestMyClass
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
[junit] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:247)
SPECIAL_PATH
属性指向类的源代码。 COMPILED_CLASSES
属性指向放置 .class
文件的位置。我需要 tests.basedir
属性,因为我在单元测试中使用它。
我做错了什么?
编辑:我还认为我应该解释 $
的排除。我将匿名类排除在外,因为它们不代表测试用例,它们仅在测试用例中使用。
最佳答案
您告诉 Junit 执行 ${COMPILED_CLASSES}/unit_tests/
中的每个测试类,但您只是将 ${COMPILED_CLASSES}
放在类路径中。您可能需要将类路径条目更改为
<pathelement location="${COMPILED_CLASSES}/unit_tests/"/>
关于java - 从 Ant 运行 JUnit 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2057152/