JUnit – Generating HTML Reports

Learn to create an HTML report for execution results of JUnit tests. In this example, I am creating an HTML report for JUnit-Examples project available on Github.

1. Maven Surefire Report Plugin

1.1. Add Plugin Dependency

Add the latest version of maven-surefire-report-plugin into reporting section of pom.xml.

<reporting>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-surefire-report-plugin</artifactId>
			<version>2.22.2</version>
		</plugin>
	</plugins>
</reporting>

1.2. Default Command

When we run the JUnit tests with Surefire plugin, it generates the reports in plain-text and XML formats.

When invoked, the Surefire Report plugin parses the generated target/surefire-reports/TEST-*.xml files and renders them using DOXIA (content generation framework), creating the test results’ HTML interface version.

Notice that by default, the plugin is not attached to any of the core phases, which we generally invoke using mvn command. So, we have to call it directly from the command line:

mvn surefire-report:report

If we open the generated HTML report, it looks something like this.

2. Cofiguration Options

2.1. Showing Only Failed Tests

By default, the Surefire Report Plugin shows all the successes and failures in the generated HTML report. To display the failed tests only, the property showSuccess should be set to false.

<reporting>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-surefire-report-plugin</artifactId>
			<version>2.22.2</version>
			<configuration>
				<showSuccess>false</showSuccess>
			</configuration>
		</plugin>
	</plugins>
</reporting>

2.2. Custom Report Name

To set the custom file name for the generated HTML report, the outputName property should be set to the desired name.

After executing mvn site, the generated report file is named JUnit-Examples-Test-Report.html.

<reporting>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-surefire-report-plugin</artifactId>
			<version>2.22.2</version>
			<configuration>
				<outputName>JUnit-Examples-Test-Report</outputName>
			</configuration>
		</plugin>
	</plugins>
</reporting>

2.3. Source Code Reference for Failed Tests

For the failed tests, we may wish to report out the line number at which they got failed. This can be done using the maven-jxr-plugin plugin.

Add this plugin into the reporting section of pom.xml file for reporting the line numbers.

<reporting>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-surefire-report-plugin</artifactId>
			<version>2.22.2</version>
			<configuration>
				<outputName>JUnit-Examples-Test-Report</outputName>
			</configuration>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jxr-plugin</artifactId>
			<version>3.1.1</version>
		</plugin>
	</plugins>
</reporting>

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.