[英]JUnit test fails in Ant with `<junit>` task but passes with `<exec>`?
我正在將我的JUnit測試自動化到我的Ant構建中。 但是,我的簡單測試僅在從IDE和命令行運行時通過,但在Ant的<junit>
任務中失敗。 當我從命令行運行它時(我在技術上使用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
但是當我使用<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
MY_TEMP
的內容是junit-4.10.jar
, SimpleTest.java
和build.xml
。
我已將junit-4.10.jar
復制到%ANT_HOME%\\lib
文件夾,如Ant junit任務文檔所示 。 它已經有ant-junit.jar
和ant-junit4.jar
。
我的Java版本是1.6.0_26。
我的測試是:
// YES, this is the default package
import org.junit.*;
public class SimpleTest {
@Test
public void mySimpleTest(){
Assert.assertEquals( 2, 1 + 1 );
}
}
我的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>
如果測試通過一種方式而另一種方式失敗,則可能與類路徑相關,例如找不到測試類,測試類或庫。
測試輸出應該有助於澄清這是否是問題所在。
添加此行以獲取更多信息:
<formatter type="brief" usefile="false"/>
具體來說,我將junit_ant
任務編輯為:
<junit>
<classpath location="." />
<test name="SimpleTest" />
<formatter type="xml" />
</junit>
<junitreport todir=".">
<fileset dir=".">
<include name="TEST-*.xml" />
</fileset>
<report todir="." />
</junitreport>
然后向我展示了失敗是java.lang.ClassNotFoundException: SimpleTest
所以我只是添加了<classpath location="." />
<classpath location="." />
到<junit>
任務,然后它工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.