Custom JUnit Report?
我正在使用ant任务" junit"和" junitreport"来运行我的JUnit测试并在最后生成一个报告(=>"单元测试结果")。
是否有一些简单的方法可以某种方式扩展此输出以使更多信息显示在报告中? 例如,添加一个附加列,其中包含测试所截取的屏幕快照的链接。
我已经看到有人可以编写自己的ant junit测试运行程序,例如EclipseTestRunner
但这是相当大的努力。 是否没有API可以访问单元报告的元素?
您可以通过使用嵌套的
1 2 | <!-- use reportstyle/junit-frames.xsl to produce the report --> <report styledir="reportstyle" format="frames" todir="testreport"/> |
为了自定义输出,一个选项是制作默认XSLT的副本并进行修改。或者,您可以寻找一种更易于自定义的替代XSLT。
对于较小的更改,最简单的方法是仅导入默认的XSLT并覆盖您需要自定义的任何模板。例如,要为每个测试添加一列,您将需要覆盖产生表标题的模板和产生表行的模板。下面,我刚刚复制了这些模板,并对其进行了一些修改,以添加一列(查找标有
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | <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><xsl:value-of select="../@name"/></td> </xsl:if> <td> <xsl:choose> <xsl:when test="boolean($show.class)"> <xsl:value-of select="@name"/> </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> screenshot </td> </tr> </xsl:template> </xsl:stylesheet> |
结果如下所示:
Jukka的精彩ans。这是Jukka关于如何精确链接屏幕截图的答案的扩展
1 2 3 4 | <!-- ADDED --> <td> screenshot </td> |
而不是在Jukka的ans中上面的代码段,这里是如何链接屏幕截图:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <!-- 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"> <xsl:value-of select="@name"/> </xsl:when> <xsl:when test="error"> <xsl:value-of select="@name"/> </xsl:when> </xsl:choose> </td> |
生成junit报告后,您需要做的就是-从junit_report目录下的" selenium / screenshots /"目录中复制所有屏幕截图。
上面的代码只会为失败的测试添加链接。如果要全部使用,则相应地修改代码。
另外,如果您不想替换主xsl文件,则可以将xsl文件复制到项目根文件夹中,使用所做的更改对其进行更新,最后通过添加styledir属性来编辑build.xml文件:
1 | <report styledir="." format="noframes" todir="${junit.output.dir}"/> |