cobertura-maven-plugin 生成单元测试报告时排除接口,异常类,常量类2013-11-17当我们为一个项目用cobertura生成单元测试覆盖率报告时,常常有以下需求:需求说明:因为一个项目有许多接口定义,常量定义,异常类定义,这些类是不需要单元测试的。而当我们用cobertura来生成测试报告时候,如果不排除这些类的话,就会使得报告的数据不太好看,因为毕竟有某些被算进去的类没有对应的单元测试嘛。所以我们就希望可以在用cobertura-maven-plugin生成测试报告时候,能自动排除这些接口,常量,异常类。解决方法:其实很简单,只要在项目的pom.xml中用到cobertura-maven-plugin的时候在<instrumentation>元素下的<excludes>元素中吧这些接口,常量,异常排除掉就可以了。为此,我们必须遵守一些约定:一个常见的排除定义如下:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>cobertura-maven-plugin</artifactId> <version>2.5.1</version> <configuration><instrumentation><!-- charles: excludes the package name which contains "interfaces","constants","exceptions"--><!--so now ,the ut test coverage should be much better --> <excludes> <exclude>com/walmart/platform/io/interfaces/*.class</exclude> <exclude>com/walmart/platform/io/constants/*.class</exclude><exculde>com/walmart/platform/io/exception/*.class</exculde> </excludes></instrumentation> </configuration> </plugin>
所以我们在这里的<exclude>里面已经把若干路径进行了排除,所以接下来我们只要在项目中吧所有的接口类放在com.walmart.platform.io.interfaces开头的包路径中,吧所有的常量类放在com.walmart.platform.io.constants开头的包路径中,吧所有的异常类放在com.walmart.platform.io.exception开头的包路径中。则当运行 mvn cobertura:cobertura时候,生成测试报告就自动排除了这3种类。