Java的测试覆盖率工具可以使用Cobertura
、Jacoco
,以下主要介绍Cobertura的配置
基础配置
cobertura-maven-plugin的主页
1 2 3 4 5 6 7 8 9
| <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>cobertura-maven-plugin</artifactId> <version>2.7</version> </plugin> </plugins> </build>
|
此时可以用mvn cobertura:cobertura
命令产生测试覆盖率的报告,报告位置在target\site\cobertura\
目录
在构建过程中校验
如果需要设置满足测试覆盖率达到某个阈值才能构建成功的话,需要做以下配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>cobertura-maven-plugin</artifactId> <version>2.7</version> <configuration> <check> <branchRate>85</branchRate> <lineRate>85</lineRate> <haltOnFailure>true</haltOnFailure> <totalBranchRate>85</totalBranchRate> <totalLineRate>85</totalLineRate> <packageLineRate>85</packageLineRate> <packageBranchRate>85</packageBranchRate> <regexes> <regex> <pattern>io.jasonlu.springrestful.*</pattern> <branchRate>90</branchRate> <lineRate>80</lineRate> </regex> </regexes> </check> </configuration> <executions> <execution> <phase>prepare-package</phase> <goals> <goal>clean</goal> <goal>check</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
|
执行mvn package
时就会进行测试覆盖率的校验