自定义JUnit报告?

bla*_*ube 31 java ant junit

我使用ant任务'junit'和'junitreport'来运行我的JUnit测试并在最后生成一个报告(=>"单元测试结果").

是否有一些简单的方法来扩展此输出以获得更多信息显示在报告中?例如,添加一个附加列,其中包含测试所截取的屏幕截图的链接.

我已经看到有人可以像EclipseTestRunner那样编写一个自己的ant junit测试运行器, 但这是相当多的努力.是否没有API来访问单位报告的元素?

Juk*_*nen 36

junitreport任务使用XSLTjunit任务生成的XML文件生成报告.

您可以使用styledir嵌套report元素的属性指定自己的XSLT来自定义输出:

<!-- use reportstyle/junit-frames.xsl to produce the report -->
<report styledir="reportstyle" format="frames" todir="testreport"/>
Run Code Online (Sandbox Code Playgroud)

为了自定义输出,一个选项是制作默认XSLT的副本并对其进行修改.或者您可以寻找替代XSLT,它更容易为您的目的定制.

对于较小的更改,最简单的方法是导入默认的XSLT并覆盖您需要自定义的模板.例如,要为每个测试添加一列,您需要覆盖生成表头的模板和生成表行的模板.下面,我刚刚复制了这些模板并对其进行了一些修改以添加一列(查找标有的两个添加项<!-- ADDED -->).

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- import the default stylesheet -->
  <xsl:import href="jar:file:lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl"/>

  <!-- override the template producing the test table header --> 
  <xsl:template name="testcase.test.header">
    <xsl:param name="show.class" select="''"/>
    <tr valign="top">
      <xsl:if test="boolean($show.class)">
        <th>Class</th>
      </xsl:if>
      <th>Name</th>
      <th>Status</th>
      <th width="80%">Type</th>
      <th nowrap="nowrap">Time(s)</th>

      <!-- ADDED -->
      <th>Screenshot</th>

    </tr>
  </xsl:template>

  <!-- override the template producing a test table row -->
  <xsl:template match="testcase" mode="print.test">
    <xsl:param name="show.class" select="''"/>
    <tr valign="top">
      <xsl:attribute name="class">
        <xsl:choose>
          <xsl:when test="error">Error</xsl:when>
          <xsl:when test="failure">Failure</xsl:when>
          <xsl:otherwise>TableRowColor</xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:variable name="class.href">
        <xsl:value-of select="concat(translate(../@package,'.','/'), '/', ../@id, '_', ../@name, '.html')"/>
      </xsl:variable>
      <xsl:if test="boolean($show.class)">
        <td><a href="{$class.href}"><xsl:value-of select="../@name"/></a></td>
      </xsl:if>
      <td>
        <a name="{@name}"/>
        <xsl:choose>
          <xsl:when test="boolean($show.class)">
            <a href="{concat($class.href, '#', @name)}"><xsl:value-of select="@name"/></a>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="@name"/>
          </xsl:otherwise>
        </xsl:choose>
      </td>
      <xsl:choose>
        <xsl:when test="failure">
          <td>Failure</td>
          <td><xsl:apply-templates select="failure"/></td>
        </xsl:when>
        <xsl:when test="error">
          <td>Error</td>
          <td><xsl:apply-templates select="error"/></td>
        </xsl:when>
        <xsl:otherwise>
          <td>Success</td>
          <td></td>
        </xsl:otherwise>
      </xsl:choose>
      <td>
        <xsl:call-template name="display-time">
          <xsl:with-param name="value" select="@time"/>
        </xsl:call-template>
      </td>

      <!-- ADDED -->
      <td>
        <a href="link/to/screenshot/for/test/{@name}">screenshot</a>
      </td>

    </tr>
  </xsl:template>

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

结果如下:

截图

  • 嗨,我不想在报告中使用不同的风格.我需要通过其他(自定义)信息扩展报告.所以我必须能够以某种方式修改报告的数据,而不是它的风格.谢谢. (2认同)
  • ...(1)每次测试将屏幕截图放入单独的目录中.然后,不是直接链接到屏幕截图,而是链接到目录. (2认同)