【发布时间】:2011-01-29 14:36:21
【问题描述】:
当 JUnit 测试在 Eclipse 中运行时引发运行时异常时,您可以看到整个堆栈跟踪。
我们的构建服务器使用 ant 并运行 JUnit,但失败时的打印输出仅提供异常消息,而不是整个“printStackTrace”。有没有方便的方法来获得这个功能?
【问题讨论】:
当 JUnit 测试在 Eclipse 中运行时引发运行时异常时,您可以看到整个堆栈跟踪。
我们的构建服务器使用 ant 并运行 JUnit,但失败时的打印输出仅提供异常消息,而不是整个“printStackTrace”。有没有方便的方法来获得这个功能?
【问题讨论】:
我已经将此答案发布到 this question,然后才意识到它与您的重复。
这是我的 junit 标记,它确实会产生异常跟踪。
<junit
showoutput="yes"
errorProperty="test.failed"
failureProperty="test.failed"
haltOnFailure="${test.halt-on-failure}"
fork="yes"
forkmode="${junit.forkmode}"
>
<classpath>
<pathelement location="${classes.dir}"/>
<pathelement location="${classes-under-test.classes.dir}" />
</classpath>
<!-- #Formatters for capture and display -->
<formatter
type="brief"
usefile="false"
/>
<formatter type="brief" />
<formatter
type="xml"
if="test.generate.xml.output"
/>
<!-- #Test case isolation technique -->
<test
name="${testcase}"
if="testcase"
/>
<batchtest
todir="${test.data.dir}"
unless="testcase"
>
<fileset dir="${classes.dir}">
<include name="**/Test*.class" />
<exclude name="**/Test*$*.class" />
</fileset>
</batchtest>
</junit>
我认为可以为你做这件事的一个嵌套元素是
<formatter
type="brief"
usefile="false"
/>
【讨论】:
JUnit BaseTestRunner 中有一个设置限制了这个值:
/**
* Truncates a String to the maximum length.
*/
public static String truncate(String s) {
if (fgMaxMessageLength != -1 && s.length() > fgMaxMessageLength)
s= s.substring(0, fgMaxMessageLength)+"...";
return s;
}
你可以通过设置来改变这个值:
BaseTestRunner.setPreference("maxmessage", "-1");
【讨论】: