1、新建一个maven工程,在src/main/java 下建一个CoverageTest.java 类
package test_junit;public class CoverageTest { public CoverageTest() { // TODO Auto-generated constructor stub } public static void main(String[] args) { // TODO Auto-generated method stub } public static int testadd(int x, int y){ int c = 0; if(x == 10){ c = x + y; }else{ c = (x + y)*2; } return c; }}
2、在src/main/java 新建一个测试类JunitTest.java
package junit;import org.junit.Assert;import org.junit.Test;import test_junit.CoverageTest;/** * Created by 000284 on 2017/2/6. */public class JunitTest { @Test public void testadd(){ int b = CoverageTest.testadd(5, 20); Assert.assertEquals(b,50); }}
3、pom.xml 文件
4.0.0 testunit test_junit 1.0-SNAPSHOT jenkins env.BUILD_NUMBER org.codehaus.mojo cobertura-maven-plugin 2.7 xml package cobertura junit junit 4.12
3、jenkins 安装插件cobertura
4、新建jenkins job
build >Goals and options 设置:clean cobertura:cobertura
Cobertura xml report pattern 设置: **/target/site/cobertura/coverage.xml
post setps 设置:Enable the "Publish Cobertura Coverage Report" publisher
5、构建job 查看 Coverage Report 就会显示覆盖率报表