To generate JUnit HTML reports from maven builds we need to add the surefire plugin to generate the xml files and then the site and reporting plugins to create the html report.
Explanation Video
Creating JUnit Surefire XML files
The JUnit HTML files are built from the .xml files created when running the test.
The surefire plugin will generate the .xml
files in your Target
folder when running mvn test
.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
</plugins>
</build>
Generating the HTML Report
To Generate the HTML report. We need to use mvn site
And in order for that to work we need to add a site build plugins and a reporting plugin.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
</plugins>
</build>
<!-- `mvn clean test site` to generate the junit html report-->
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
</plugins>
</reporting>
This will work for both JUnit 4 and Junit 5.
mvn clean test site
I generally run mvn clean test site
so that I don’t have any previous .xml
files messing up the report.
Official Documentation
- https://maven.apache.org/surefire/maven-surefire-plugin/
- https://maven.apache.org/surefire/maven-surefire-report-plugin/
- https://maven.apache.org/plugins/maven-site-plugin/
Source Example
You can find a project with a pom.xml
that does this over here:
And the pom.xml
NOTES: I orginally thought I needed to add the Maven Project Info Reports Plugin in
build
orreporting
but this seems to be added through a transitive dependency and is mentioned in the build any, so I haven’t listed it in thepom.xml
. Leaving this as a note to self in case it turns out to be important. I wanted to try and create a minimalpom.xml
but if you have issues feel free to try adding it.<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.0.0</version> </plugin>