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 時,表示覆蓋率只能越來越高,不能低。 反正就會有容錯率。

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM