Jenkins使用jacoco外掛程式檢測程式碼涵蓋範圍

來源:互聯網
上載者:User

Jenkins使用jacoco外掛程式檢測程式碼涵蓋範圍

程式碼涵蓋範圍:類覆蓋,方法覆蓋,行覆蓋,指令覆蓋……(簡而言之,就是判斷有沒有被執行)

覆蓋率 = 已經執行的代碼 / 總代碼

(1)建立maven項目,配置pom.xml如下

pom.xml

<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>cn.demo</groupId>
    <artifactId>answers</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>answers</name>
    <url>http://maven.apache.org</url>
 
    <build>
        <finalName>answers</finalName>
        <plugins>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${compiler.source}</source>
                    <target>${compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
           
            <!--檢測程式碼涵蓋範圍的外掛程式-->
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.8</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
           
        </plugins>
    </build>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <compiler.source>1.7</compiler.source>
        <compiler.target>1.7</compiler.target>
        <junit.version>4.12</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.8</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>       
</project>

(2)下載jacoco-plugin外掛程式

在jenkins的可選外掛程式中,選中  jacoco-plugin 外掛程式 直接下載

(3)jenkins使用jacoco-plugin外掛程式構建項目的配置

在構建後操作中 --》 選中  Record JaCoCo coverage report  開始詳細配置

Path to exec files:target/jacoco.exec    #這裡指定你的jacoco.exec檔案的位置,檔案名稱必須是 jacoco.exec ,,否則會出錯 ,這裡最好配置為

Path to class directories: target/classes      #這裡配置原始碼的位元組碼檔案目錄位置

Path to source directories :src/main/java      #這裡配置為源碼的目錄位置

Inclusion : 標明還需要檢測的檔案

Exclusions:標明需要除外的檔案(不想被檢測的檔案)

下面的值都是屬於 1-100 (代表程式碼涵蓋範圍)

口 Change build status according the thresholds  #選中這裡可以改變項目的構建狀態

(烏雲數必須小於太陽數 ,所有的值必須小於100 ,大於的話系統會自動化佈建為100)

當項目的真實程式碼涵蓋範圍 小於太陽所標明的值時,項目會構建不穩定 黃色 unstable

這裡 %Method 對應太陽這一列的值 設定為 100:表示每個方法都要被執行,整個項目才能穩定構建;只要有一個方法沒有被執行,整個項目就會構建不穩定。

注釋:項目的真實程式碼涵蓋範圍 jenkins會計算出來的

比如: 當你的method那一列 太陽對應的值為 100 ,而你的項目中 總共有10個方法,其中有8個被執行了,還有2個沒有被執行,那麼你的真實程式碼涵蓋範圍為 80,這時候整個項目構建結果為 unstable (黃色標識)

口 fail the build if the coverage degrades more than the delta thresholds   #這是和上一次構建的程式碼涵蓋範圍做對比的

選中後,並且全部值設定為0 。如果本次構建的覆蓋率低於上次的覆蓋率,整個項目就會構建失敗

這些值都可以改變,但是設定為0 時,表示覆蓋率只能越來越高,不能低。 反正就會有容錯率。

到這裡,可以正常構建了。

本文永久更新連結地址:https://www.bkjia.com/Linux/2018-03/151154.htm

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.