Generating test reports for JUnit tests helps you understand test results, track failures, analyze trends, and share results with your team or CI/CD pipeline. Here’s a comprehensive guide covering the most common approaches.
1. Using Maven Surefire Plugin (Default Reports)
The Maven Surefire Plugin automatically generates test reports when you run tests.
Basic Configuration
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
</plugins>
</build>
Running Tests and Generating Reports
mvn test
After execution, reports are generated in:
target/surefire-reports/— Contains.txtand.xmlfiles for each test class.
2. Generating HTML Reports with Maven Surefire Report Plugin
For human-readable HTML reports, use the Surefire Report Plugin.
Configuration
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.2.5</version>
</plugin>
</plugins>
</reporting>
Generate the HTML Report
mvn surefire-report:report
Or generate a full site report:
mvn site
The HTML report will be available at:
target/site/surefire-report.html
3. Using Gradle for Test Reports
Gradle generates HTML and XML test reports automatically when you run tests.
Enable Reports in build.gradle
plugins {
id 'java'
}
test {
useJUnitPlatform()
reports {
html.required = true
junitXml.required = true
}
// Optional: customize output location
// reports.html.outputLocation = layout.buildDirectory.dir("customReports/html")
}
Run Tests
gradle test
Reports are generated at:
- HTML:
build/reports/tests/test/index.html - XML:
build/test-results/test/
4. Using JUnit 5 with the Console Launcher
For standalone test execution, JUnit 5 provides a console launcher that can produce XML reports.
java -jar junit-platform-console-standalone.jar \
--class-path target/classes:target/test-classes \
--scan-class-path \
--reports-dir=target/junit-reports
5. Using Allure for Rich Reports
Allure provides beautiful, interactive test reports with detailed insights.
Maven Configuration
<dependencies>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-junit5</artifactId>
<version>2.27.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/1.9.22/aspectjweaver-1.9.22.jar"
</argLine>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.22</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Annotate Your Tests
import io.qameta.allure.Description;
import io.qameta.allure.Epic;
import io.qameta.allure.Feature;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@Epic("Calculator")
@Feature("Basic Operations")
class CalculatorTest {
@Test
@Description("Verify that adding two positive numbers returns their sum")
void addTwoNumbers() {
Calculator calc = new Calculator();
assertEquals(5, calc.add(2, 3));
}
}
Generate and View Report
mvn test
allure serve target/allure-results
6. Using IntelliJ IDEA Built-in Test Reports
IntelliJ IDEA has excellent built-in reporting. When you run tests through the IDE:
- Run your tests (right-click test class → Run).
- In the Run tool window, click the Export Test Results icon (or use Ctrl+Alt+E / Cmd+Option+E to view options).
- Choose format: HTML, XML, or Custom.
You can also import external test results:
- Run → Import Tests from File…
7. Publishing Reports in CI/CD
GitHub Actions Example
name: Java CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v4
with:
java-version: '25'
distribution: 'temurin'
- name: Run tests
run: mvn test
- name: Publish Test Report
uses: mikepenz/action-junit-report@v4
if: always()
with:
report_paths: '**/target/surefire-reports/TEST-*.xml'
- name: Upload Test Results
uses: actions/upload-artifact@v4
if: always()
with:
name: test-results
path: target/surefire-reports/
Jenkins Example
Add a post-build step to publish JUnit test results:
post {
always {
junit 'target/surefire-reports/*.xml'
}
}
Summary of Report Locations
| Tool | Command | Report Location |
|---|---|---|
| Maven Surefire | mvn test |
target/surefire-reports/ |
| Surefire Report Plugin | mvn surefire-report:report |
target/site/surefire-report.html |
| Gradle | gradle test |
build/reports/tests/test/index.html |
| Allure | allure serve target/allure-results |
Interactive browser view |
| IntelliJ IDEA | Run tests in IDE | Export via Run tool window |
Best Practices
- Always publish XML reports in CI/CD — they are machine-readable and widely supported.
- Use HTML reports for local development and code reviews.
- Consider Allure for larger projects where detailed insights (trends, categories, steps) are valuable.
- Fail the build on test failures to prevent broken code from being deployed.
- Archive test reports as CI artifacts for historical reference.
Would you like a deeper dive into any specific reporting tool, or help setting one up in your project?
