【问题标题】:How to get full stack traces logged when a JUnit test running in ant fails?当在 ant 中运行的 JUnit 测试失败时,如何记录完整的堆栈跟踪?
【发布时间】:2011-01-29 14:36:21
【问题描述】:

当 JUnit 测试在 Eclipse 中运行时引发运行时异常时,您可以看到整个堆栈跟踪。

我们的构建服务器使用 ant 并运行 JUnit,但失败时的打印输出仅提供异常消息,而不是整个“printStackTrace”。有没有方便的方法来获得这个功能?

【问题讨论】:

    标签: ant junit


    【解决方案1】:

    我已经将此答案发布到 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"
      />
    

    【讨论】:

      【解决方案2】:

      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");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-16
        • 2011-09-08
        • 1970-01-01
        • 1970-01-01
        • 2014-01-06
        • 1970-01-01
        • 2018-04-24
        • 2021-03-15
        相关资源
        最近更新 更多