java JUnit 没有提供有关“错误”的信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/327324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:58:20  来源:igfitidea点击:

JUnit gives no information regarding "errors"

javaantjunit

提问by user41762

I'm using Junit 4.4 and Ant 1.7. If a test case fails with an error (for example because a method threw an unexpected exception) I don't get any details about what the error was.

我正在使用 Junit 4.4 和 Ant 1.7。如果测试用例因错误而失败(例如,因为某个方法抛出了意外异常),我将无法获得有关错误是什么的任何详细信息。

My build.xml looks like this:

我的 build.xml 看起来像这样:

<target name="test" depends="compile">
<junit printsummary="withOutAndErr" filtertrace="no" fork="yes" haltonfailure="yes" showoutput="yes">
  <classpath refid="project.run.path"/>
  <test name="a.b.c.test.TestThingee1"/>
  <test name="a.b.c.test.NoSuchTest"/>
</junit>
</target>

When I run "ant test" it says (for example) 2 Test runs, 0 failures, 1 error. It doesn't say "There is no such test as NoSuchTest" even though this is completely reasonable and would let me figure out the cause of the error.

当我运行“蚂蚁测试”时,它说(例如)2 次测试运行,0 次失败,1 次错误。它没有说“没有像 NoSuchTest 这样的测试”,即使这是完全合理的,并且可以让我找出错误的原因。

Thanks!

谢谢!

-Dan

-担

回答by user41762

Figured it out :)

弄清楚了 :)

I needed to add a "formatter" inside the junit block.

我需要在 junit 块中添加一个“格式化程序”。

<formatter type="plain" usefile="false" />

What a PITA.

什么皮塔饼。

-Dan

-担

回答by Jeffrey Fredrick

If you're going to have a lot of tests there are two change you might want to consider:

如果您要进行大量测试,您可能需要考虑两个更改:

  1. run all the tests rather than stopping at the first error
  2. create a report showing all the test results
  1. 运行所有测试而不是在第一个错误处停止
  2. 创建显示所有测试结果的报告

And it is pretty easy to do with the junitreport task:

使用 junitreport 任务很容易做到:

<target name="test">
    <mkdir dir="target/test-results"/>
    <junit fork="true" forkmode="perBatch" haltonfailure="false"
           printsummary="true" dir="target" failureproperty="test.failed">
        <classpath>
            <path refid="class.path"/>
            <pathelement location="target/classes"/>
            <pathelement location="target/test-classes"/>
        </classpath>
        <formatter type="brief" usefile="false" />
        <formatter type="xml" />
        <batchtest todir="target/test-results">
            <fileset dir="target/test-classes" includes="**/*Test.class"/>
        </batchtest>
    </junit>

    <mkdir dir="target/test-report"/>
    <junitreport todir="target/test-report">
        <fileset dir="target/test-results">
            <include name="TEST-*.xml"/>
        </fileset>
        <report format="frames" todir="target/test-report"/>
    </junitreport>

    <fail if="test.failed"/>
</target>