• Cobertura + jenkins 单元测试代码覆盖率统计


    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 文件

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>testunit</groupId>
        <artifactId>test_junit</artifactId>
        <version>1.0-SNAPSHOT</version>
     <profiles>
            <!-- Jenkins by default defines a property BUILD_NUMBER which is used to enable the profile. -->
            <profile>
                <id>jenkins</id>
                <activation>
                    <property>
                        <name>env.BUILD_NUMBER</name>
                    </property>
                </activation>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>cobertura-maven-plugin</artifactId>
                            <version>2.7</version>
                            <configuration>
                                <formats>
                                    <format>xml</format>
                                </formats>
                            </configuration>
                            <executions>
                                <execution>
                                    <phase>package</phase>
                                    <goals>
                                        <goal>cobertura</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>
        </profiles>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
        </dependencies>
    
    </project>

    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 就会显示覆盖率报表

  • 相关阅读:
    设置 添加 erlang 代码路径 工作路径
    [转]inline,__inline,__forceinline 关于函数内联及相关关键字的详细说明
    [转]Delphi 2010 3513正式版破解
    IBM developerWorks 文章转载系列(二)
    Cassandra和HBase主要设计思路对比
    Storm : Twitter的实时数据处理工具(转载)
    Oracle NoSQL Database (转载)
    MapReduce Hold不住? (转载)
    [InfoQ]Twitter Storm:开源实时Hadoop (转载)
    数据分析与处理之一(大规模数据分析架构)
  • 原文地址:https://www.cnblogs.com/testway/p/6380656.html
Copyright © 2020-2023  润新知