Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven and code metrics: check for existence of a test case for each class

Is there something that can be used in Maven to automate this kind of check? I'm seeing checkstyle and PMD but I'm not finding this feature.

Basically I'd like the build to fail if there's a class A and there's not an ATestCase. I know, it is not a strict check and can be easily bypassed by creating just the class, but at the moment that would be enough.

like image 239
Andrea Avatar asked Nov 12 '13 07:11

Andrea


1 Answers

What ou are looking for

As Jens Piegsa pointed id out, what you are looking for is a tool that show you the test coverage, in other words the percentage of code which is used by you tests.

It allow you to see how much you code is tested, in a really more reliable way than (at least test by class).

You can use Cobertura, which well integrated in Maven: http://mojo.codehaus.org/cobertura-maven-plugin/

The way to achieve that

POM Configuration

Just put this code snippet in your pom.xml

<project>
  ...  
  <reporting>
    <plugins>
      ...
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.6</version>
      </plugin>
    </plugins>
  </reporting>
</project>

Running coverage

And run

 mvn cobertura:cobertura

Or run the report phase (binded with site generation)

 mvn site:site

Adding quality Threshold

You can even add failing threshold if you want to invalidate low coverage builds

    <plugin>
         [...]
         <configuration>
            <check>
                <!-- Fail if code coverage does not respects the goals  -->
                <haltOnFailure>true</haltOnFailure>
                <!-- Per-class thresholds -->
                <lineRate>80</lineRate>
                <!-- Per-branch thresholds (in a if verify that if and else are covered-->
                <branchRate>80</branchRate>
                <!-- Project-wide thresholds -->
                <totalLineRate>90</totalLineRate>
                <totalBranchRate>90</totalBranchRate>
            </check>
        </configuration>
    </plugin>
like image 72
Jean-Rémy Revy Avatar answered Sep 28 '22 04:09

Jean-Rémy Revy