单元测试仅在从 Ant 任务运行时失败 [英] Unit test fails only when running from with Ant task

查看:28
本文介绍了单元测试仅在从 Ant 任务运行时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 eclipse 和 Ant 任务运行相同的测试.从 Eclipse 运行时,所有测试都通过.当我运行 Ant junit 任务时,一个单一的测试失败并出现以下奇怪的错误:

I run the same tests from both eclipse and from an Ant task. When running from eclipse all tests pass. When I run Ant junit task, one, single test fails with the following strange error:

junit.framework.AssertionFailedError在 org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)在 org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:423)在 org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:137)在 unitests.mypackage.MyTestClass.myTestCase(来源不明)

junit.framework.AssertionFailedError at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32) at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:423) at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:137) at unitests.mypackage.MyTestClass.myTestCase(Unknown Source)

可能是什么原因?

我读了一点,发现可能是因为eclipse和Ant使用了不同版本的junit.在我的项目中,junit 位于 libs/junit-4.10.jar 并在 eclipse 的 .classpath 文件和 junit 任务类路径中引用.你可以在这里看到 Ant 的任务:

I read a little and found out that it might be because eclipse and Ant use different versions of junit. In my project, junit is located at libs/junit-4.10.jar and referenced both in eclipse's .classpath file and in junit task classpath. You can see Ant's task here:

<path id="classpath">
    <fileset dir="${lib.dir}" includes="**/*.jar"/>
    <fileset dir="${src.dir}" includes="**/*.jar"/>
</path>
...
<target name="run-unit-tests" depends="compile,compile-unit-tests">
    <mkdir dir="${junit.output.dir}"/>
    <junit fork="yes" printsummary="yes" haltonfailure="no">          
        <classpath>
            <path refid="classpath"/>
            <fileset dir="${unit.tests.classes.dir}" includes="**/*.class"/>
        </classpath>

        <formatter type="xml"/>
        <batchtest todir="${junit.output.dir}">
            <fileset dir="${unit.tests.dir}">
                <include name="**/*Test*.java"/>
            </fileset>
        </batchtest>
    </junit>

    <mkdir dir="${junit.report.dir}"/>
    <junitreport todir="${junit.report.dir}">
      <fileset dir="${junit.output.dir}">
        <include name="TEST-*.xml"/>
      </fileset>
      <report format="frames" todir="${junit.report.dir}/html"/>
    </junitreport>
</target>

Ant 的版本是 1.7.1,它带有 Eclipse.

The version of Ant is 1.7.1 and it came with eclipse.

最终通过将 fork="yes" 添加到 junit 的任务来解决它.通过使用 eclipse 的导出选项生成构建文件,然后查看生成的文件和我的文件之间的差异,找到了它.不知道为什么分叉可以解决问题.

Eventually solved it by adding fork="yes" to junit's task. Found it by generating build file using eclipse's export option, and then looking at the differences between the generated file and mine. No idea why forking solves the problem though.

推荐答案

当堆栈跟踪显示 org.eclipse 条目时,我强烈怀疑您正在通过 Eclipse 运行 ant.如果你从控制台运行 ant,你很可能不会遇到这个问题.

As the stack trace shows org.eclipse entries, I strongly suspect you are running ant via Eclipse. If you run ant from the console you will very likely not have this problem.

因此,原因是 Eclipse 中的错误.这应该不足为奇,Eclipse 充满了错误.

The cause, therefore, is a bug in Eclipse. This should be no surprise, Eclipse is full of bugs.

fork="true" 有帮助,因为它会导致 ant 产生一个新的执行进程.这个新进程在类路径上没有任何 Eclipse 类.

fork="true" helps because it causes ant to spawn a new process for execution. This new process does not have any Eclipse classes on the classpath.

这篇关于单元测试仅在从 Ant 任务运行时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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