单元测试只从Ant任务运行时出现故障 [英] Unit test fails only when running from with Ant task

查看:494
本文介绍了单元测试只从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是位于库/ JUnit的-4.10.jar和Eclipse的.classpath文件,并在junit任务的classpath引用两者。
你可以在这里看到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>

蚂蚁的版本是1.7.1,并与日食来了。

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

编辑:

最终加入解决了叉=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.

叉=真正的帮助,因为它会导致蚂蚁产卵执行一个新的进程。这种新工艺没有在类路径中任何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天全站免登陆