How can I generate an HTML report for Junit results?
Asked Answered
I

8

64

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.

PS: Given the project structure I am not supposed to use Ant :(

Intertwist answered 5/3, 2010 at 9:4 Comment(2)
What (build) technology are you currently using? If you don't provide any hints about this, there are just too many answers!Procurer
the easiest I have come across is an node module called junit-viewer npmjs.com/package/ecolutis-junit-viewerLyrate
T
16

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.

Timmytimocracy answered 8/3, 2010 at 13:48 Comment(1)
Thanks for your answer! I will try to convince the staff that we need ANT, and the whole idea of an IDE-Centric project is bad.Intertwist
H
47

I found the above answers quite useful but not really general purpose, they all need some other major build system like Ant or 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

You can install it by doing:

pip install junit2html
Hindemith answered 21/6, 2016 at 20:38 Comment(5)
Thank you very much, I was looking for a simple tool like that! As the answer does not show a usage example: After installation simply call junit2html results.xml output.html.Inch
This sounds great, but my junit xml test results are in many separate .xml files. Do folks who have used junit2html know if it supports multiple .xml files like the ant example shown by RPM below? If it doesn't, how do you deal with multiple .xml test result files (> 100)Schnurr
Answering my own question after playing with it a little more: merge multiple .xml files into single .xml file by passing name of a directory (not a file) junit2html test-results --merge test-results\reports.xml then convert single .xml into an .html file junit2html test-results\reports.xml test-results\reports.html. My examples here are using Windows path syntax)Schnurr
Or for an overall summary, use junit2html test-results\reports.xml --report-matrix test-results\matrix.html for a report that summarizes the results from multiple tests in a easier to read way. Nice work folks.Schnurr
@Hindemith why isn't --merge documented? It's great!Cresol
Z
18

Alternatively for those using Maven build tool, there is a plugin called Surefire Report.

The report looks like this : Sample

Zambrano answered 3/6, 2010 at 19:56 Comment(0)
T
16

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.

Timmytimocracy answered 8/3, 2010 at 13:48 Comment(1)
Thanks for your answer! I will try to convince the staff that we need ANT, and the whole idea of an IDE-Centric project is bad.Intertwist
S
12

You can easily do this via ant. Here is a build.xml file for doing this

 <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.

To run this (assuming the above file is called buildTestReports.xml) run the following command in the terminal:

ant -buildfile buildTestReports.xml
Screwworm answered 23/1, 2015 at 20:23 Comment(2)
That worked, thank you! I just wish I could sort by the "Time" column in the resulting table.Hurter
I tried to use this to collect newman junit reports, but it didn't work, git error - is not a valid testsuite XML document. Did u face this ?Balanchine
H
9

Junit xml format is used outside of Java/Maven/Ant word. Jenkins with http://wiki.jenkins-ci.org/display/JENKINS/xUnit+Plugin is a solution.

For the one shot solution I have found this tool that does the job: https://www.npmjs.com/package/junit-viewer

junit-viewer --results=surefire-reports --save=file_location.html

--results= is directory with xml files (test reports)

Hatty answered 8/8, 2016 at 11:41 Comment(3)
junit-viewer does not support placeholder in the results path.Vauban
this isn't working, on running junit-viewer shows html on terminal but doesn't save it anywhere.Balanchine
some failures may be avoided by adding --minify-false to junit-viewer argumentsDenounce
H
6

I found xunit-viewer, which has deprecated junit-viewer mentioned by @daniel-kristof-kiss.

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.

I use it to upload test results from Travis builds as Travis has no other support for collecting standard formatted test results output.

Haydenhaydn answered 1/8, 2019 at 7:7 Comment(0)
B
4

There are multiple options available for generating HTML reports for Selenium WebDriver scripts.

1. Use the JUNIT TestWatcher class for creating your own Selenium HTML reports

The TestWatcher JUNIT class allows overriding the failed() and succeeded() JUNIT methods that are called automatically when JUNIT tests fail or pass.

The TestWatcher JUNIT class allows overriding the following methods:

  • protected void failed(Throwable e, Description description)

failed() method is invoked when a test fails

  • protected void finished(Description description)

finished() method is invoked when a test method finishes (whether passing or failing)

  • protected void skipped(AssumptionViolatedException e, Description description)

skipped() method is invoked when a test is skipped due to a failed assumption.

  • protected void starting(Description description)

starting() method is invoked when a test is about to start

  • protected void succeeded(Description description)

succeeded() method is invoked when a test succeeds

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

Allure framework can help with generating HTML reports for your Selenium WebDriver projects.

The reporting framework is very flexible and it works with many programming languages and unit testing frameworks.

You can read everything about it at https://allurereport.org/.

You will need the following dependencies and plugins to be added to your pom.xml file

  1. maven surefire
  2. aspectjweaver
  3. allure adapter

See more details including code samples on this article: http://test-able.blogspot.com/2015/10/create-selenium-html-reports-with-allure-framework.html

Bally answered 22/10, 2015 at 22:6 Comment(0)
O
1

I have created a JUnit parser/viewer that runs directly in the browser. It supports conversion to JSON and the HTML report can be easily reused.

https://lotterfriends.github.io/online-junit-parser/

If you are still missing a feature feel free to create an issue on Github. :)

Oceanid answered 29/3, 2022 at 11:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.