Maven Unit Test Report
Jakob Jenkov |
Maven can generate unit test reports in HTML. Maven does so by running the unit tests and recording the results of the unit tests. Maven then generates an HTML report from the unit test results.
Using Maven to generate unit test reports can be useful to see what unit tests fails during the build. Especially if the build is big and takes a long time, because then you might not run all the unit tests from inside your IDE during development. You might only run the unit tests relevant to the changes you make. Then, to check that everything in the project still works, you can tell Maven to run the unit tests and generate a unit test report. You can then read the report to see if any of the unit test have failed during the build.
The Maven unit test report is an HTML file. That means that it can be copied to a server if you need to share it with other developers.
The Maven Surefire Plugin
The Maven unit test reports are generated by the Maven Surefire plugin. Therefore a unit test report is also some times referred to as surefire report.
Generating a Unit Test Report
You generate a Maven unit test report (Surefire unit test report) using the following Maven command:
mvn surefire-report:report
For more information about how the Maven command structure looks, see my Maven commands tutorial.
The generated unit test report can be found in the target/site
directory. The unit test report is
named surefire-report.html
. The path to the unit test report is thus:
your-project/target/site/surefire-report.html
Skipping the Tests
Sometimes you might want Maven to generate a unit test report without running all the unit tests again. You might just want to use the results from the last run. You can get Maven to just generate the unit test report without rerunning the unit tests using this command:
mvn surefire-report:report-only
This command will only generate the unit test report.
If you want to run Maven completely without running any tests or test report, you can do so using this command:
mvn install -Dmaven.test.skip=true
It is the argument -Dmaven.test.skip=true
that makes Maven skip the tests completely.
Enable Java Preview Features During Test Runs
If you are using any of the Java preview features in Java 12, 13, 14, 15 etc. , the Surefire plugin will fail to run the tests unless you tell Surefire to run the tests with Java preview features enabled.
To configure the Maven Surefire plugin to enable Java preview features you need the following configuration
in your Maven POM file - in the <build><plugins>...</plugins></build>
section:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration> <argLine>--enable-preview</argLine> </configuration> </plugin>
Tweet | |
Jakob Jenkov |