[英]JUnit test fails in Ant with `<junit>` task but passes with `<exec>`?
I am automating my JUnit tests into my Ant build. 我正在将我的JUnit测试自动化到我的Ant构建中。 However, my simple test only passes when run from the IDE and commandline but fails with Ant's
<junit>
task. 但是,我的简单测试仅在从IDE和命令行运行时通过,但在Ant的
<junit>
任务中失败。 When I run it from the commandline (I am technically using the Ant <exec>
task) the result is: 当我从命令行运行它时(我在技术上使用Ant
<exec>
任务),结果是:
clean:
compile_tests:
[javac] Compiling 2 source files to C:\MY_TEMP
junit_exec:
[exec] JUnit version 4.10
[exec] .
[exec] Time: 0.004
[exec]
[exec] OK (1 test)
[exec]
BUILD SUCCESSFUL
Total time: 1 second
but when I use the <junit>
task: 但是当我使用
<junit>
任务时:
Buildfile: C:\MY_TEMP\build.xml
clean:
compile_tests:
[javac] Compiling 2 source files to C:\MY_TEMP
junit_ant:
[echo] junit_ant started
[junit] Test SimpleTest FAILED
BUILD SUCCESSFUL
Total time: 0 seconds
The contents of MY_TEMP
are junit-4.10.jar
, SimpleTest.java
, and build.xml
. MY_TEMP
的内容是junit-4.10.jar
, SimpleTest.java
和build.xml
。
I have copied junit-4.10.jar
to the %ANT_HOME%\\lib
folder as suggest by the Ant junit task documentation . 我已将
junit-4.10.jar
复制到%ANT_HOME%\\lib
文件夹,如Ant junit任务文档所示 。 It already had both ant-junit.jar
and ant-junit4.jar
. 它已经有
ant-junit.jar
和ant-junit4.jar
。
My version of Java is 1.6.0_26. 我的Java版本是1.6.0_26。
My test is: 我的测试是:
// YES, this is the default package
import org.junit.*;
public class SimpleTest {
@Test
public void mySimpleTest(){
Assert.assertEquals( 2, 1 + 1 );
}
}
And my Ant file (build.xml ), is: 我的Ant文件(build.xml)是:
<?xml version="1.0"?>
<project name="regression_tests" basedir=".">
<target name="clean">
<delete>
<fileset dir="." includes="*.class" />
</delete>
</target>
<target name="compile_tests" depends="clean">
<javac srcdir="." destdir="." source="1.6" target="1.6" includeantruntime="false" >
<classpath>
<pathelement location="./junit-4.10.jar" />
</classpath>
</javac>
</target>
<target name="junit_ant" depends="compile_tests" >
<echo message="junit_ant started" />
<junit>
<test name="SimpleTest" />
</junit>
</target>
<target name="junit_exec" depends="compile_tests">
<exec executable="java" dir="." >
<arg value="-classpath" />
<arg value=".;junit-4.10.jar" />
<arg value="org.junit.runner.JUnitCore" />
<arg value="SimpleTest" />
</exec>
</target>
</project>
If a test passes one way, and fails another, it's likely something classpath-related, like it can't find a test class, a class under test, or a library. 如果测试通过一种方式而另一种方式失败,则可能与类路径相关,例如找不到测试类,测试类或库。
The test output should help clarify if this is what the issue is. 测试输出应该有助于澄清这是否是问题所在。
添加此行以获取更多信息:
<formatter type="brief" usefile="false"/>
Specifically I editted my junit_ant
task to: 具体来说,我将
junit_ant
任务编辑为:
<junit>
<classpath location="." />
<test name="SimpleTest" />
<formatter type="xml" />
</junit>
<junitreport todir=".">
<fileset dir=".">
<include name="TEST-*.xml" />
</fileset>
<report todir="." />
</junitreport>
Which then showed me that the failure was java.lang.ClassNotFoundException: SimpleTest
so I just added <classpath location="." />
然后向我展示了失败是
java.lang.ClassNotFoundException: SimpleTest
所以我只是添加了<classpath location="." />
<classpath location="." />
to the <junit>
task and then it worked. <classpath location="." />
到<junit>
任务,然后它工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.