【问题标题】:Custom JUnit Report?自定义 JUnit 报告?
【发布时间】:2010-12-16 04:54:09
【问题描述】:

我正在使用 ant 任务“junit”和“junitreport”来运行我的 JUnit 测试并在最后生成报告(=>“单元测试结果”)。

是否有一些简单的方法可以以某种方式扩展此输出以在报告中显示更多信息?例如,添加一个附加列,其中包含指向测试截取的屏幕截图的链接。

我看到有人可以编写自己的 ant junit 测试运行程序,例如 EclipseTestRunner 但这是相当大的努力。是否没有 API 可以访问单元报告的元素?

【问题讨论】:

  • 您要寻找什么样的屏幕截图?你不只是想要错误的堆栈跟踪,你正在寻找控制台,或者它是一个图形表示?
  • 您好,我不想知道如何获取屏幕截图。我已经做到了这一点。我需要知道的是如何扩展 JUnit 报告,以便包含指向我的屏幕截图的链接。谢谢。
  • 最新的 jUnit 版本没有新功能了吗?

标签: java ant junit


【解决方案1】:

Jukka 的精彩回答。这是 Jukka 关于如何链接屏幕截图的答案的扩展

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

在 Jukka 的 ans 中,不是上面的 sn-p,而是链接屏幕截图的方法:

    <!-- Added screenshot link for failed tests -->
    <td>
        <xsl:variable name="class.name">
            <xsl:value-of select="translate(@classname,'.','/')"/>
        </xsl:variable>
        <xsl:variable name="junit.base">
            <xsl:call-template name="path"><xsl:with-param name="path" select="../@package"/></xsl:call-template>
        </xsl:variable>
    <xsl:choose>
        <xsl:when test="failure">
            <a href="{concat($junit.base,$class.name,'/',@name,'.png')}"><xsl:value-of select="@name"/></a>
        </xsl:when>
        <xsl:when test="error">
            <a href="{concat($junit.base,$class.name,'/',@name,'.png')}"><xsl:value-of select="@name"/></a>
        </xsl:when>
    </xsl:choose>
    </td>

生成 junit 报告后,您需要做的就是 - 从 junit_report 目录下的“selenium/screenshots/”目录中复制所有屏幕截图。

上面的代码只会为失败的测试添加链接。如果你想要它,那么相应地修改代码。

【讨论】:

    【解决方案2】:

    另外,如果您不想替换主 xsl 文件,您可以将 xsl 文件复制到项目根文件夹中,使用您的更改更新它,最后编辑您的 build.xml 文件添加 styledir属性:

    <report styledir="." format="noframes" todir="${junit.output.dir}"/>
    

    【讨论】:

      【解决方案3】:

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

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

      <!-- use reportstyle/junit-frames.xsl to produce the report -->
      <report styledir="reportstyle" format="frames" todir="testreport"/>
      

      要自定义输出,一种选择是复制default XSLT 并对其进行修改。或者,您可以寻找更容易根据您的目的进行定制的替代 XSLT。

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

      <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>
      

      结果如下所示:

      【讨论】:

      • 嗨,我不想在我的报告中应用不同的样式。我需要通过其他(自定义)信息来扩展报告。所以我必须能够以某种方式修改报告的数据,而不是它的样式。谢谢。
      • junitreport 任务生成的报告的源数据来自junit 任务生成的 XML 文件。但是,根据这些数据,您可以在报告中添加一些额外的信息。 -- 例如,如果您的单元测试在某个已知位置生成屏幕截图,其名称可以从单元测试名称派生而来,则可以通过自定义报告样式来创建指向它们的链接,如上例所示。 (我编辑了答案以添加最终结果的屏幕截图。)
      • 感谢您的提议!这是一个很好的开始方法。只是每个测试方法我不仅有一个屏幕截图,而且还有几个屏幕截图,具体取决于测试的内容。所以截图是编号和命名的,例如测试类-testMethod-01.png。 XSLT 有没有办法“计算”文件系统上“属于”测试方法的屏幕截图数量?。
      • 如果不使用扩展,从 XSLT 访问文件系统实际上是不可行的。可以从 XSLT 1 访问的唯一格式大约是(毫不奇怪)XML。目前我想到了三个选项,从需要最少编程的一个到需要最多编程的一个:...
      • ... (1) 将屏幕截图放入每个测试的单独目录中。然后,不是直接链接到屏幕截图,而是链接到目录。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-24
      相关资源
      最近更新 更多