0
0
JUnittesting~5 mins

XML and HTML reports in JUnit

Choose your learning style9 modes available
Introduction

XML and HTML reports show test results clearly. They help you understand which tests passed or failed.

After running automated tests to see detailed results.
When sharing test results with your team or manager.
To keep a record of test runs for future reference.
To find out which tests need fixing quickly.
When integrating tests with other tools that read XML or HTML.
Syntax
JUnit
mvn test
mvn surefire-report:report

mvn test runs your tests and creates XML reports by default.

mvn surefire-report:report generates an HTML report from the XML files.

Examples
This command runs tests and creates XML reports in target/surefire-reports.
JUnit
mvn test
This command creates a readable HTML report from the XML files.
JUnit
mvn surefire-report:report
This plugin runs tests and generates XML reports automatically.
JUnit
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.0.0-M7</version>
</plugin>
Sample Program

This simple test class has two tests that pass. Running mvn test creates XML reports. Then mvn surefire-report:report creates an HTML report you can open in a browser.

JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class SimpleTest {

    @Test
    void addition() {
        assertEquals(2, 1 + 1);
    }

    @Test
    void subtraction() {
        assertEquals(0, 1 - 1);
    }
}

// Run tests with: mvn test
// Then generate HTML report with: mvn surefire-report:report
OutputSuccess
Important Notes

XML reports are machine-readable and used by many tools.

HTML reports are easy to read for humans in a web browser.

Always check the target/surefire-reports folder for reports after tests run.

Summary

XML reports store test results in a structured format.

HTML reports show test results in a friendly way.

Use Maven commands to generate both reports easily.