有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java自定义JUnit报告?

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

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

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


共 (3) 个答案

  1. # 1 楼答案

    Jukka的真棒ans。这是Jukka关于如何链接截图的答案的延伸

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

    以下是如何链接截图,而不是Jukka的ans中的上述片段:

        <!  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_报告目录下的“selenium/screenshots/”目录复制所有屏幕截图

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

  2. # 2 楼答案

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

    <report styledir="." format="noframes" todir="${junit.output.dir}"/>
    
  3. # 3 楼答案

    ^{}任务使用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并覆盖需要自定义的任何模板可能是最简单的。例如,要为每个测试添加一列,需要覆盖生成表头的模板和生成表行的模板。下面,我刚刚复制了这些模板,并对它们进行了一些修改,添加了一列(查找两个带有<! 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>
    

    结果如下:

    screenshot