0
0
JUnittesting~15 mins

XML and HTML reports in JUnit - Build an Automation Script

Choose your learning style9 modes available
Generate and verify JUnit XML and HTML test reports
Preconditions (2)
Step 1: Create a simple JUnit test class with one passing and one failing test method
Step 2: Run the tests using Maven or Gradle command
Step 3: Locate the generated XML report file in the target/surefire-reports directory
Step 4: Open the XML report and verify it contains test results with correct test names and statuses
Step 5: Generate an HTML report using a reporting plugin or tool (e.g., Maven Surefire Report Plugin)
Step 6: Open the HTML report in a browser
Step 7: Verify the HTML report shows the test results summary with passed and failed tests clearly indicated
✅ Expected Result: JUnit XML report file is created with correct test results; HTML report displays a clear summary of test outcomes including passed and failed tests
Automation Requirements - JUnit 5 with Maven Surefire Plugin
Assertions Needed:
Verify test methods pass or fail as expected
Verify XML report file exists after test run
Verify XML report content includes test names and statuses
Verify HTML report file exists after report generation
Verify HTML report content includes summary of test results
Best Practices:
Use descriptive test method names
Use Maven Surefire Plugin for consistent report generation
Use assertions to validate test outcomes
Check file existence and parse XML report for validation
Keep reports in standard directories for easy access
Automated Solution
JUnit
package com.example;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

public class SampleTest {

    @Test
    void testThatPasses() {
        assertTrue(5 > 1, "5 is greater than 1");
    }

    @Test
    void testThatFails() {
        assertEquals(2, 1 + 1, "1 + 1 should equal 2");
        fail("Intentional failure for demonstration");
    }
}

/*
To run and generate reports:
1. Run tests with Maven:
   mvn clean test

2. XML reports will be in target/surefire-reports/*.xml

3. To generate HTML report, add to pom.xml:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-report-plugin</artifactId>
      <version>3.0.0-M7</version>
      <executions>
        <execution>
          <phase>verify</phase>
          <goals>
            <goal>report</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

4. Run:
   mvn verify

5. HTML report will be in target/site/surefire-report.html

6. Open the HTML report in a browser to see test summary.
*/

This JUnit 5 test class has two test methods: one that passes and one that fails intentionally.

Running mvn clean test executes the tests and generates XML reports in the target/surefire-reports directory.

The Maven Surefire Report Plugin is configured to generate an HTML report during the verify phase, which you run with mvn verify. The HTML report is saved in target/site/surefire-report.html.

Opening the HTML report in a browser shows a clear summary of passed and failed tests, making it easy to understand test results.

This setup follows best practices by using descriptive test names, standard Maven directories, and built-in plugins for report generation.

Common Mistakes - 4 Pitfalls
Not configuring the Maven Surefire Plugin to generate reports
Using vague or non-descriptive test method names
Ignoring the location of generated reports
Not verifying the content of XML or HTML reports
Bonus Challenge

Now add data-driven testing with 3 different input values and verify reports reflect all test cases

Show Hint