如何为 Junit 结果生成 HTML 报告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2385553/
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
How can I generate an HTML report for Junit results?
提问by Andrei Ciobanu
Is there a way to (easily) generate a HTML report that contains the tests results ? I am currently using JUnit in addition to Selenium for testing web apps UI.
有没有办法(轻松)生成包含测试结果的 HTML 报告?除了 Selenium 之外,我目前还使用 JUnit 来测试 Web 应用程序 UI。
PS: Given the project structure I am not supposed to use Ant :(
PS:鉴于项目结构,我不应该使用 Ant :(
采纳答案by Dave Hunt
If you coulduse Ant then you would just use the JUnitReport task as detailed here: http://ant.apache.org/manual/Tasks/junitreport.html, but you mentioned in your question that you're not supposed to use Ant. I believe that task merely transforms the XML report into HTML so it would be feasible to use any XSLT processor to generate a similar report.
如果您可以使用 Ant,那么您只需使用此处详述的 JUnitReport 任务:http://ant.apache.org/manual/Tasks/junitreport.html ,但您在问题中提到您不应该使用 Ant . 我相信该任务只是将 XML 报告转换为 HTML,因此使用任何 XSLT 处理器来生成类似的报告都是可行的。
Alternatively, you could switch to using TestNG ( http://testng.org/doc/index.html) which is very similar to JUnit but has a default HTML report as well as several other cool features.
或者,您可以切换到使用 TestNG ( http://testng.org/doc/index.html),它与 JUnit 非常相似,但具有默认的 HTML 报告以及其他几个很酷的功能。
回答by IanNorton
I found the above answers quite useful but not really general purpose, they all need some other major build system like Ant or Maven.
我发现上述答案非常有用,但并非真正通用,它们都需要其他一些主要的构建系统,如 Ant 或 Maven。
I wanted to generate a report in a simple one-shot command that I could call from anything (from a build, test or just myself) so I have created junit2html which can be found here: https://github.com/inorton/junit2html
我想用一个简单的一次性命令生成一份报告,我可以从任何东西(来自构建、测试或只是我自己)调用它,所以我创建了 junit2html,可以在这里找到:https: //github.com/inorton/ junit2html
You can install it by doing:
您可以通过执行以下操作来安装它:
pip install junit2html
回答by h3xStream
Alternatively for those using Maven build tool, there is a plugin called Surefire Report.
或者,对于那些使用 Maven 构建工具的人,有一个名为Surefire Report的插件。
The report looks like this : Sample
该报告如下所示:示例
回答by RPM
You can easily do this via ant. Here is a build.xml file for doing this
您可以通过 ant 轻松完成此操作。这是用于执行此操作的 build.xml 文件
<project name="genTestReport" default="gen" basedir=".">
<description>
Generate the HTML report from JUnit XML files
</description>
<target name="gen">
<property name="genReportDir" location="${basedir}/unitTestReports"/>
<delete dir="${genReportDir}"/>
<mkdir dir="${genReportDir}"/>
<junitreport todir="${basedir}/unitTestReports">
<fileset dir="${basedir}">
<include name="**/TEST-*.xml"/>
</fileset>
<report format="frames" todir="${genReportDir}/html"/>
</junitreport>
</target>
</project>
This will find files with the format TEST-*.xml and generate reports into a folder named unitTestReports.
这将找到格式为 TEST-*.xml 的文件并将报告生成到名为 unitTestReports 的文件夹中。
To run this (assuming the above file is called buildTestReports.xml) run the following command in the terminal:
要运行此文件(假设上述文件名为 buildTestReports.xml),请在终端中运行以下命令:
ant -buildfile buildTestReports.xml
回答by Daniel Kristof Kiss
Junit xml format is used outside of Java/Maven/Ant word. Jenkins with http://wiki.jenkins-ci.org/display/JENKINS/xUnit+Pluginis a solution.
Junit xml 格式在 Java/Maven/Ant word 之外使用。Jenkins 与http://wiki.jenkins-ci.org/display/JENKINS/xUnit+Plugin是一个解决方案。
For the one shot solution I have found this tool that does the job: https://www.npmjs.com/package/junit-viewer
对于一次性解决方案,我发现这个工具可以完成这项工作:https: //www.npmjs.com/package/junit-viewer
junit-viewer --results=surefire-reports --save=file_location.html
junit-viewer --results=surefire-reports --save=file_location.html
--results=
is directory with xml files (test reports)
--results=
是包含 xml 文件的目录(测试报告)
回答by thoni56
I found xunit-viewer
, which has deprecated junit-viewer
mentioned by @daniel-kristof-kiss.
我发现xunit-viewer
,@daniel-kristof-kissjunit-viewer
提到的已弃用。
It is very simple, automatically recursively collects all relevant files in ANT Junit XML format and creates a single html-file with filtering and other sweet features.
它非常简单,以 ANT Junit XML 格式自动递归收集所有相关文件,并创建一个具有过滤和其他甜蜜功能的单个 html 文件。
I use it to upload test results from Travis builds as Travis has no other support for collecting standard formatted test results output.
我使用它从 Travis 构建上传测试结果,因为 Travis 没有其他支持收集标准格式的测试结果输出。
回答by Alex Siminiuc
There are multiple options available for generating HTML reports for Selenium WebDriver scripts.
有多种选项可用于为 Selenium WebDriver 脚本生成 HTML 报告。
1. Use the JUNIT TestWatcher class for creating your own Selenium HTML reports
1. 使用 JUNIT TestWatcher 类创建您自己的 Selenium HTML 报告
The TestWatcher JUNIT class allows overriding the failed() and succeeded() JUNIT methods that are called automatically when JUNIT tests fail or pass.
TestWatcher JUNIT 类允许覆盖 failed() 和 successed() JUNIT 方法,这些方法在 JUNIT 测试失败或通过时自动调用。
The TestWatcher JUNIT class allows overriding the following methods:
TestWatcher JUNIT 类允许覆盖以下方法:
- protected void failed(Throwable e, Description description)
- protected void failed(Throwable e, Description description)
failed() method is invoked when a test fails
failed() 方法在测试失败时调用
- protected void finished(Description description)
- 受保护的无效完成(说明描述)
finished() method is invoked when a test method finishes (whether passing or failing)
当测试方法完成时(无论是通过还是失败)调用finished()方法
- protected void skipped(AssumptionViolatedException e, Description description)
- protected void skipped(AssumptionViolatedException e, Description description)
skipped() method is invoked when a test is skipped due to a failed assumption.
由于假设失败而跳过测试时会调用 skipped() 方法。
- protected void starting(Description description)
- 受保护的无效开始(说明描述)
starting() method is invoked when a test is about to start
当测试即将开始时调用starting()方法
- protected void succeeded(Description description)
- 受保护的无效成功(说明描述)
succeeded() method is invoked when a test succeeds
successed() 方法在测试成功时调用
See below sample code for this case:
对于这种情况,请参见下面的示例代码:
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class TestClass2 extends WatchManClassConsole {
@Test public void testScript1() {
assertTrue(1 < 2); >
}
@Test public void testScript2() {
assertTrue(1 > 2);
}
@Test public void testScript3() {
assertTrue(1 < 2);
}
@Test public void testScript4() {
assertTrue(1 > 2);
}
}
import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
public class WatchManClassConsole {
@Rule public TestRule watchman = new TestWatcher() {
@Override public Statement apply(Statement base, Description description) {
return super.apply(base, description);
}
@Override protected void succeeded(Description description) {
System.out.println(description.getDisplayName() + " " + "success!");
}
@Override protected void failed(Throwable e, Description description) {
System.out.println(description.getDisplayName() + " " + e.getClass().getSimpleName());
}
};
}
2. Use the Allure Reporting framework
2. 使用魅力报告框架
Allure framework can help with generating HTML reports for your Selenium WebDriver projects.
Allure 框架可以帮助为您的 Selenium WebDriver 项目生成 HTML 报告。
The reporting framework is very flexible and it works with many programming languages and unit testing frameworks.
报告框架非常灵活,它适用于许多编程语言和单元测试框架。
You can read everything about it at http://allure.qatools.ru/.
您可以在http://allure.qatools.ru/阅读有关它的所有内容。
You will need the following dependencies and plugins to be added to your pom.xml file
您需要将以下依赖项和插件添加到您的 pom.xml 文件中
- maven surefire
- aspectjweaver
- allure adapter
- 行家万无一失
- aspectjweaver
- 诱惑适配器
See more details including code samples on this article: http://test-able.blogspot.com/2015/10/create-selenium-html-reports-with-allure-framework.html
在本文中查看更多详细信息,包括代码示例:http: //test-able.blogspot.com/2015/10/create-selenium-html-reports-with-allure-framework.html