JUnit 测试在 Ant 中使用 `<junit>` 任务失败,但通过 `<exec>`? [英] JUnit test fails in Ant with `<junit>` task but passes with `<exec>`?

查看:29
本文介绍了JUnit 测试在 Ant 中使用 `<junit>` 任务失败,但通过 `<exec>`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 JUnit 测试自动化到我的 Ant 构建中.但是,我的简单测试仅在从 IDE 和命令行运行时通过,但在 Ant 的 <junit> 任务中失败.当我从命令行运行它时(我在技术上使用 Ant <exec> 任务),结果是:

I am automating my JUnit tests into my Ant build. However, my simple test only passes when run from the IDE and commandline but fails with Ant's <junit> task. When I run it from the commandline (I am technically using the Ant <exec> task) the result is:

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:

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.jarSimpleTest.javabuild.xml.

我已将 junit-4.10.jar 复制到 %ANT_HOME%\lib 文件夹,如 Ant junit 任务文档.它已经有 ant-junit.jarant-junit4.jar.

I have copied junit-4.10.jar to the %ANT_HOME%\lib folder as suggest by the Ant junit task documentation. It already had both ant-junit.jar and ant-junit4.jar.

我的 Java 版本是 1.6.0_26.

My version of Java is 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)是:

And my Ant file (build.xml ), is:

<?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.

这篇关于JUnit 测试在 Ant 中使用 `&lt;junit&gt;` 任务失败,但通过 `&lt;exec&gt;`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆