How to Generate Test Report in JUnit Easily
To generate a test report in
JUnit, run your tests using a build tool like Maven or Gradle which automatically creates XML reports. Alternatively, use IDEs or CI tools that parse these reports to show test results clearly.Syntax
JUnit itself runs tests and produces results, but test reports are generated by tools that run JUnit tests. Commonly, Maven or Gradle are used to run tests and generate reports in XML or HTML format.
Example Maven command to run tests and generate reports:
mvn test- runs tests and creates reports intarget/surefire-reports
Example Gradle command:
gradle test- runs tests and generates reports inbuild/reports/tests/test
bash
mvn test gradle test
Example
This example shows a simple JUnit 5 test class and how to run it with Maven to generate a test report.
The report will be saved automatically by Maven in target/surefire-reports as XML and plain text files.
java
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class CalculatorTest { @Test void addition() { assertEquals(5, 2 + 3); } @Test void subtraction() { assertEquals(1, 3 - 2); } }
Output
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running CalculatorTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec
Results :
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
Common Pitfalls
- Not running tests via build tools: Running tests directly in IDE may not generate reports automatically.
- Missing plugins: Maven requires
maven-surefire-pluginto run tests and generate reports. - Report location confusion: Reports are usually in
target/surefire-reports(Maven) orbuild/reports/tests/test(Gradle). - JUnit version mismatch: Use JUnit 5 dependencies and plugins compatible with your build tool.
xml
/* Wrong: Running tests without Maven or Gradle - no reports generated */ /* Right: Use Maven with surefire plugin configured in pom.xml */ <project> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M7</version> </plugin> </plugins> </build> </project>
Quick Reference
| Command | Description | Report Location |
|---|---|---|
| mvn test | Run tests and generate reports with Maven | target/surefire-reports |
| gradle test | Run tests and generate reports with Gradle | build/reports/tests/test |
| IDE run | Run tests in IDE (may not generate reports) | Depends on IDE settings |
Key Takeaways
Use Maven or Gradle to run JUnit tests and automatically generate test reports.
Test reports are saved as XML and HTML files in standard build directories.
Ensure build plugins like maven-surefire-plugin are configured for report generation.
Running tests only in IDE may not produce test reports unless configured.
Check report folders after test runs to review detailed test results.