0
0
JUnittesting~8 mins

XML and HTML reports in JUnit - Framework Patterns

Choose your learning style9 modes available
Framework Mode - XML and HTML reports
Folder Structure
project-root/
├── src/
│   └── test/
│       └── java/
│           └── com/example/tests/
│               ├── LoginTest.java
│               └── UserProfileTest.java
├── test-results/
│   ├── xml/
│   │   └── TEST-com.example.tests.LoginTest.xml
│   └── html/
│       └── index.html
├── pom.xml
└── junit-platform.properties

This structure separates source tests and test results. XML reports go to test-results/xml, HTML reports to test-results/html.

Test Framework Layers
  • Test Layer: Contains JUnit test classes under src/test/java.
  • Reporting Layer: JUnit generates XML reports automatically after test runs.
  • HTML Report Generator: A plugin or tool (like Maven Surefire or Jenkins) converts XML reports to HTML format.
  • Configuration Layer: Controls report output locations and formats via pom.xml or junit-platform.properties.
  • Utilities Layer: Optional helper classes for custom report formatting or test listeners.
Configuration Patterns
  • Maven Surefire Plugin: Configure in pom.xml to enable XML report generation and specify output folder.
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.0.0-M7</version>
      <configuration>
        <reportsDirectory>${project.basedir}/test-results/xml</reportsDirectory>
      </configuration>
    </plugin>
  • JUnit Platform Properties: Use junit-platform.properties to customize test execution and reporting.
    junit.platform.output.capture.stdout=true
    junit.platform.output.capture.stderr=true
  • HTML Report Generation: Use tools like maven-surefire-report-plugin or CI tools to convert XML to HTML.
  • Environment Handling: Use Maven profiles or environment variables to switch report output paths or enable/disable reports.
Test Reporting and CI/CD Integration
  • JUnit XML Reports: Automatically generated after tests run, contain detailed test results, failures, and stack traces.
  • HTML Reports: Generated by plugins or CI tools from XML reports for easy human reading with colors and summaries.
  • CI/CD Integration: Configure Jenkins, GitHub Actions, or GitLab CI to collect XML reports and publish HTML reports as build artifacts or dashboards.
  • Notifications: Use test report results to trigger notifications on failures or successes.
  • Historical Tracking: Store reports in CI to track test trends over time.
Best Practices
  1. Separate Test Code and Reports: Keep test source code and test reports in different folders for clarity.
  2. Use Standard Report Formats: XML reports follow JUnit schema for compatibility with many tools.
  3. Automate HTML Report Generation: Use Maven plugins or CI tools to convert XML to HTML automatically after tests.
  4. Configure Reports per Environment: Use profiles or properties to customize report generation for local, staging, or production tests.
  5. Integrate with CI/CD: Always publish reports in CI pipelines for visibility and quick feedback.
Self Check

Where in this folder structure would you add a new XML report for a test class named PaymentTest.java?

Key Result
JUnit test framework generates XML reports automatically; HTML reports are created by plugins or CI tools for easy viewing.