Generating code coverage reports for a site

Let us now include code coverage from the unit tests of our project in the site documentation.

How to do it...

Use the following steps to generate code coverage reports for a site:

  1. Open the Maven project for which you want to do this (for instance, project-with-documentation).
  2. Add the following code in the <build> section of the pom.xml file:
        <plugin>
          <groupId>org.jacoco</groupId>
          <artifactId>jacoco-maven-plugin</artifactId>
          <version>0.7.2.201409121644</version>
          <executions>
              <execution>
              <id>default-prepare-agent</id>
              <goals>
                  <goal>prepare-agent</goal>
              </goals>
              </execution>
          </executions>
        </plugin>
  3. Add the following code in the reporting section of the pom.xml file:
    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.2.201409121644</version>
    </plugin>
  4. Run the following Maven command:
    mvn test site
    
  5. Observe the site report as shown in following screenshot:
    How to do it...

How it works...

The JaCoCo unit test coverage report shows up in site documentation on account of the following issues:

  • As the prepare-agent goal of the JaCoCo plugin is added to the build section, Maven runs the JaCoCo agent
  • As the test goal is run, Maven runs the test and the agent analyzes the tests for coverage
  • As the JaCoCo plugin is added to the reporting section of the pom.xml file, the coverage report is generated and linked to the site documentation
  • As you can see, the same plugin is added to the build and reporting section and does different things

There's more...

If you were to use Cobertura instead of JaCoCo to generate test coverage, you could do the following:

  1. Remove the lines related to JaCoCo in the build and reporting sections.
  2. Add the following code to the reporting section of the pom.xml file:
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.6</version>
          </plugin>
  3. Run the following Maven command:
    mvn site
    
  4. Open the site documentation:
    There's more...

You will notice two things:

  • We didn't need to specify anything in the build section
  • We didn't need to run the test goal explicitly; the Maven Cobertura plugin did this.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.123.189