How to Generate an HTML Report for Junit Results

How can I generate an HTML report for Junit results?

If you could use 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.

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.

How to create an HTML report for JUnit 5 tests?

UPDATE

Gradle 4.6 provides built-in support for the JUnit Platform which allows you to run JUnit Jupiter tests using the standard Gradle test task which generates HTML reports out of the box.


Answer for Gradle versions prior to 4.6

The JUnit Platform Gradle Plugin generates JUnit 4 style XML test reports.

These XML files are output to build/test-results/junit-platform by default.

So, if your build server knows how to parse JUnit 4 style XML reports, you can just point it to the XML files in that directory and let the build server generate the HTML report for you.

However, if you are asking if Gradle can generate an HTML report for your tests run via the junitPlatformTest task, then the answer is "No, unfortunately not." The reason is that the standard Gradle test task only generates HTML reports based on its own proprietary "binary" report format. Since the junitPlatformTest task does not generate reports in Gradle's binary format, Gradle itself cannot generate HTML reports for JUnit Platform tests.

Having said that, however, there is in fact a work around: you can use Ant within your Gradle build. Ant has a task for aggregating JUnit 4 based XML reports and generating an HTML report from those aggregated reports. The output is not very modern, but it is at least human readable. The downside is that the default XSLT stylesheet does not display the test class names for tests run via the JUnit Platform.

In any case, you can configure Ant's JUnitReport task in Gradle as follows.

junitPlatform {
// configure as normal
}

configurations {
junitXmlToHtml
}

task generateHtmlTestReports << {
def reportsDir = new File(buildDir, 'test-reports')
reportsDir.mkdirs()

ant.taskdef(
name: 'junitReport',
classname: 'org.apache.tools.ant.taskdefs.optional.junit.XMLResultAggregator',
classpath: configurations.junitXmlToHtml.asPath
)

ant.junitReport(todir: "$buildDir/test-results/junit-platform", tofile: "aggregated-test-results.xml") {
fileset(dir: "$buildDir/test-results/junit-platform")
report(format: 'frames', todir: reportsDir)
}
}

afterEvaluate {
def junitPlatformTestTask = tasks.getByName('junitPlatformTest')
generateHtmlTestReports.dependsOn(junitPlatformTestTask)
check.dependsOn(generateHtmlTestReports)
}

dependencies {
// configure as normal ...

junitXmlToHtml 'org.apache.ant:ant-junit:1.9.7'
}

Then, executing gradle check will generate an HTML report in build/test-reports/index.html.

Regards,

Sam (Core JUnit 5 committer)

how to generate html report if my Junit is not run by Ant but by JunitCore.run

In fact I solved the problem myself in this way:

First I use https://code.google.com/p/reporting-junit-runner/source/browse/trunk/src/junitrunner/XmlWritingListener.java?spec=svn2&r=2
to create TESTS-*.xml
Then I write the following code myself to create TEST-SUITE.xml and junit-noframes.html. The idea is make use of API of ANT to create reports without really running test. so far the solution works for me.

      Project project = new Project();
//a fake project feeding to ANT API so that latter not complain
project.setName("dummy");
project.init();

FileSet fs = new FileSet();
fs.setDir(new File(reportToDir));
fs.createInclude().setName("TEST-*.xml");
XMLResultAggregator aggregator = new XMLResultAggregator();
aggregator.setProject(project);
aggregator.addFileSet(fs);
aggregator.setTodir(new File(reportToDir));
//create TESTS-TestSuites.xml
AggregateTransformer transformer = aggregator.createReport();
transformer.setTodir(new File(reportToDir));
Format format = new Format();
format.setValue(AggregateTransformer.NOFRAMES);
transformer.setFormat(format);
//create junit-noframe.html
aggregator.execute();

how can i create a html report for the junit xml report manually?

Not 100% sure what you're asking but heck here's my ant code for doing a JUnit batch test then a HTML report using the XML formatter...

   <junit showoutput="on" printsummary="on" fork="false" haltonfailure="false"
failureproperty="unittest.failure">
<classpath>
<pathelement path="${build.classpath}"/>
<pathelement path="${classes}"/>
</classpath>
<batchtest todir="${unittests.results}">
<fileset dir="${classes}">
<include name="${batchtest.prefix}@{test}_test.class" />
</fileset>
<formatter type="xml"/>
</batchtest>
</junit>
<junitreport todir="${unittests.results}">
<fileset dir="${unittests.results}"/>
<report todir="${unittests.results}"/>
</junitreport>

note that the @{test} is because its part of a macrodef within the build.xml file.

From what you said in the question, its unsure if you're using the <formatter type="xml">.

Anyway hope that helps

oh and dont mix your slashes ${reports}\html\" > ${reports}/html/"

How to send junit report via email as report doesn't display

The format="frames"attribute normally generates separate files for frames and maybe stylesheets. As mentioned in the task documentation, you can set the value to noframes and Ant will generate the report in a single HTML:

The noframes format does not use redirecting and generates one file called junit-noframes.html.

In case you need to customize the XSL used to generate the report, you can override it using the styledir attribute (note that the file must be named junit-noframes.xsl). The default XSL is embedded in the Ant source code and can be viewed here.

Generating HTML report of testcases results in SoapUI

Yes, it is possible to generate Junit Style HTML reports using SoapUI Opensource Edition as well.

All you need to do is the execution of tests has to be done

  • use Apache-Ant software, more details on installing and configuring here
  • write build script

Here is the sample build script(build.xml):

Note that modify the SOAPUI_HOME(or define environment variable), soapui project file path, results directory path according to your environment.

<project basedir="." default="testreport" name="ant script for testing soapui project">
<property environment="env"/>
<property name="soapui.project" value="/app/demo-soapui-project.xml"/>
<property name="results.dir" value="/tmp/results"/>
<property name="reports.dir" value="${results.dir}/Reports"/>
<property name="html.dir" value="${reports.dir}/html"/>
<target name="execute.project">
<exec dir="${env.SOAPUI_HOME}" executable="testrunner.sh">
<arg line="-raj -f ${results.dir} ${soapui.project}" />
</exec>
</target>
<target name="testreport" depends="execute.project">
<mkdir dir="${reports.dir}"/>
<junitreport todir="${reports.dir}">
<fileset dir="${results.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${html.dir}" />
</junitreport>
</target>
</project>

and execute following command (run soapui project and generate report):

ant

There is also simple way (i.e., every thing configured and readily available envrionment) if you are willing to use this docker image.

Short video also available there on how to.



Related Topics



Leave a reply



Submit