Java 自定义 JUnit 报告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1727616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom JUnit Report?
提问by blackicecube
I am using the ant tasks 'junit' and 'junitreport' to run my JUnit Tests and generate a report at the end (=> "Unit Test Results").
我正在使用 ant 任务“junit”和“junitreport”来运行我的 JUnit 测试并在最后生成报告(=>“单元测试结果”)。
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.
是否有一些简单的方法可以以某种方式扩展此输出以在报告中显示更多信息?例如,添加一个附加列,其中包含指向测试截取的屏幕截图的链接。
I've seen that one could write an own ant junit test runner like the EclipseTestRunnerbut this is quite some effort. Is there no API to access the elements of a unit report?
我已经看到有人可以编写自己的 ant junit 测试运行程序,例如EclipseTestRunner,但这需要一些努力。是否没有 API 来访问单元报告的元素?
采纳答案by Jukka Matilainen
The junitreport
task uses XSLTto produce the report from the XML files generated by the junit
task.
该junitreport
任务使用XSLT从junit
任务生成的 XML 文件生成报告。
You can customize the output by specifying your own XSLT using the styledir
attribute of the nested report
element:
您可以通过使用styledir
嵌套report
元素的属性指定您自己的 XSLT 来自定义输出:
<!-- use reportstyle/junit-frames.xsl to produce the report -->
<report styledir="reportstyle" format="frames" todir="testreport"/>
For customizing the the output, one option would be to make a copy of the default XSLTand modify that. Or you could look for an alternative XSLT which is more easy to customize for your purposes.
为了自定义输出,一种选择是制作默认 XSLT的副本并对其进行修改。或者您可以寻找替代 XSLT,它更容易根据您的目的进行定制。
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 -->
).
对于小的更改,最简单的方法可能是导入默认的 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>
Here's how the result looks like:
结果如下所示:
回答by Ostap Elyashevskyy
Also if you don't want to replace the main xsl file, you can copy xsl file into the project root folder, update it with your changes and finally edit your build.xml file adding styledirattribute:
此外,如果您不想替换主 xsl 文件,您可以将 xsl 文件复制到项目根文件夹中,用您的更改更新它,最后编辑您的 build.xml 文件,添加styledir属性:
<report styledir="." format="noframes" todir="${junit.output.dir}"/>
回答by dganesh2002
Awesome ans by Jukka. This is an extension to Jukka's answer as to how exactly you can link the screenshot
Jukka 的精彩回答。这是 Jukka 关于如何链接屏幕截图的回答的扩展
<!-- ADDED -->
<td>
<a href="link/to/screenshot/for/test/{@name}">screenshot</a>
</td>
Instead of above snippet in Jukka's ans, here is how you can link the screenshots :
代替 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>
All you need to do after the junit report is generated is - copy all the screenshots from "selenium/screenshots/" directory right under junit_report directory.
在生成 junit 报告后,您需要做的就是 - 从 junit_report 目录下的“selenium/screenshots/”目录中复制所有屏幕截图。
The above code will add link only for failed tests. If you want it for all then modify code accordingly.
上面的代码只会为失败的测试添加链接。如果你想要它,那么相应地修改代码。