自JUnit的报告? [英] Custom JUnit Report?

查看:251
本文介绍了自JUnit的报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the ant tasks 'junit' and 'junitreport' to run my JUnit Tests and generate a report at the end (=> "Unit Test Results").

是不是有一些简单的方法来扩展不知何故此输出以获得在报表中显示的更多信息?例如,要添加包含链接到由测试采取截图的附加列。

Is it there some easy way to extend this output somehow to get more information displayed in the report? For example to add an additional column which contains link to a screenshot taken by the test.

我见过一个人可以写一个自己的Ant JUnit测试亚军像 EclipseTestRunner
但是这是相当一些努力。有没有API来访问一个单位报告的要素是什么?

I've seen that one could write an own ant junit test runner like the EclipseTestRunner but this is quite some effort. Is there no API to access the elements of a unit report?

推荐答案

junitreport 任务使用 XSLT 以从由<$生成的XML文件的报告C $ C>的JUnit 任务。

您可以通过使用嵌套的报告元素的 styledir 属性,指定自己的XSLT定制输出:

You can customize the output by specifying your own XSLT using the styledir attribute of the nested report element:

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

有关自定义输出,一种选择是使的<一个副本href=\"http://svn.apache.org/viewvc/ant/core/tags/ANT_171/src/etc/junit-frames.xsl?view=markup\">default XSLT 和修改。或者你可以找一个替代XSLT这样更容易定制你的目的。

For customizing the the output, one option would be to make a copy of the default XSLT and modify that. Or you could look for an alternative XSLT which is more easy to customize for your purposes.

对于小的变化,这可能是最简单的,只是导入默认XSLT和覆盖任何你需要的模板定制。例如,要添加一列用于每个测试,则需要覆盖其产生表头和其产生的表行模板的模板。下面,我刚才复制的模板和修改他们一点增加一列(查找标有&LT两个加法;! - 添加 - &GT; )<。 / p>

For small changes, it might be easiest to just import the default XSLT and override whatever templates you need to customize. For example, to add a column for each test, you would need to override the template which produces the table header and the template which produces a table row. Below, I have just copied those templates and modified them a bit to add one column (look for two additions marked with <!-- 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>

下面是结果的样子:

这篇关于自JUnit的报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆