JaCoCo 代碼覆蓋率工具(基于Maven+TestNG)

JaCoco是一個代碼覆蓋率庫。

官方網(wǎng)站:http://www.jacoco.org/

安裝:

以 Maven(http://www.testclass.net/maven/) 安裝為例:

<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.11</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.9</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>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

使用:

Maven項目目錄如下:

image

創(chuàng)建被測試類

Count.java

public class Count {

    /**
     * 計算并返回兩個參數(shù)的和
     */
    public int add(int x ,int y){
        return x + y;
    }

    /**
     * 計算并返回兩個參數(shù)的和
     */
    public int sub(int x ,int y){
        return x - y;
    }

}

代碼很簡單,這里不做過多解釋。

接下來創(chuàng)建測試類CountTest.java。

import org.testng.annotations.Test;

import static org.testng.AssertJUnit.assertEquals;


public class CountTest {

    @Test
    public void testAdd() {
        Count count = new Count();
        int result = count.add(2,2);
        assertEquals(result, 4);
    }

}

通過TestNG單元測試框架來運(yùn)行測試用例,注意這里只編寫了針對Count類的add()方法進(jìn)行測試。

運(yùn)行:

切換到j(luò)acocoTest項目根目錄下,執(zhí)行mvn install命令。

image

查看:

切換到項目下的“\target\site\jacoco\”目錄,打開 index.html文件。

image
image

通過JaCoCo工具分析可以清楚地看哪些代碼被執(zhí)行了,而哪些未被執(zhí)行。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,733評論 25 709
  • 原來我有碼農(nóng)基因,只是我從未相信和正視過這一點(diǎn)。 初中時我的數(shù)理化都非常好,到了高中立體幾何和解析幾何成了我的致命...
    正宗毛利大五郎閱讀 299評論 0 2
  • 魚兒不明白,如果條紋真的愛上了斑點(diǎn),飛鳥真的愛上魚而魚兒真的愛上水,那又該如何呢? 她吐著泡泡對水說:“我愛你!”...
    星噠閱讀 342評論 0 1

友情鏈接更多精彩內(nèi)容