IDEA Coverage runner IntelliJ IDEA 和 JaCoCo的区别

1,763 阅读4分钟

1、代码覆盖率理解

代码覆盖(Code coverage)是软件测试中的一种度量,描述程序中源代码被测试的比例和程度,所得比例称为代码覆盖率。

简单来理解,就是单元测试中代码执行量与代码总量之间的比率。


2、Java常用的单元测试覆盖率框架:

2.1: JaCoCo - Java Code Coverage Library

2.2: EMMA

2.3: Cobertura


3、IDEA 如何指定 Coverage runner 模式

image.png

image.png

image.png


4、IntelliJ IDEA 和 JaCoCo的区别

4.1、单条件对比:if (a > 5 )

public static void test(int a) {
    if (a > 5 ) {
        System.out.println("输出");
    }
}

@Test
public void test(){
    StringMethod.test(4);
}

image.png

image.png

image.png

总结:

1、JaCoCo: 一个判断算2个分支、

2、IDEA:一个判断算一个分支、

3、IDEA默认使用IDEA runner

public static void test(int a) {
    if (a > 5 ) {
        System.out.println("输出");
    }
}

@Test public void test() {
    StringMethod.test(4);
}
@Test public void test1() {
    StringMethod.test(5);
}

image.png

image.png

总结迭代补充:

1、JaCoCo: 一个判断算2个分支、

2、IDEA:一个判断算一个分支、

3、IDEA默认使用IDEA runner

4、分支只有TRUE、FALSE两种情况、不管你是否临界值那是业务层、分支不关心、所以此时追加Case无用

public static void test(int a) {
    if (a > 5 ) {
        System.out.println("输出");
    }
}

@Test public void test() {
    StringMethod.test(4);
}
@Test public void test1() {
    StringMethod.test(5);
}
@Test public void test2() {
    StringMethod.test(6);
}

image.png

image.png

总结迭代补充:

1、JaCoCo: 一个判断算2个分支、判断任一覆盖一个条件就算覆盖一个分支

2、IDEA:一个判断算一个分支、判断要覆盖全部条件才算覆盖一个分支

3、IDEA默认使用IDEA runner

4、分支只有TRUE、FALSE两种情况、不管你是否临界值那是业务层、分支不关心、所以此时追加Case = 5无用

4.2、单条件对比:if (a >= 5 )

 public static void test(int a) {
     if (a >= 5) {
         System.out.println("输出");
     }
 }
 
@Test public void test() {
    StringMethod.test(4);
}

image.png

image.png

 public static void test(int a) {
     if (a >= 5) {
         System.out.println("输出");
     }
 }
 
@Test public void test() {
    StringMethod.test(4);
}
@Test public void test1() {
    StringMethod.test(5);
}

image.png

image.png

 public static void test(int a) {
     if (a >= 5) {
         System.out.println("输出");
     }
 }
 
@Test public void test() {
    StringMethod.test(4);
}
@Test public void test1() {
    StringMethod.test(5);
}
@Test public void test2() {
    StringMethod.test(6);
}

image.png

image.png 总结:

1、更加验证了、分支只有TRUE、FALSE两种情况、不管你是否临界值、那是业务层、分支不关心、TRUE 和 FALSE 都走了就算分支覆盖全了

4.3、if (5 <= a && a <= 10)

public static void test(int a) {
    if (5 <= a && a <= 10) {
        System.out.println("输出");
    }
}

@Test public void test() {
    StringMethod.test(4);
}

image.png

image.png

public static void test(int a) {
    if (5 <= a && a <= 10) {
        System.out.println("输出");
    }
}

@Test public void test() {
    StringMethod.test(4);
}
@Test public void test1() {
    StringMethod.test(5);
}

image.png

image.png

if (5 <= a && a <= 10) {
    System.out.println("输出");
    System.out.println("输出");
    System.out.println("输出");
}

@Test public void test() {
    StringMethod.test(4);
}
@Test public void test1() {
    StringMethod.test(5);
}
@Test public void test2() {
    StringMethod.test(11);
}

image.png

image.png

总结:

4.4、if (5 <= a && a <= 10 && a != 8)

if (5 <= a && a <= 10 && a != 8) {
    System.out.println("输出");
    System.out.println("输出");
    System.out.println("输出");
}

@Test public void test() {
    StringMethod.test(4);
}

image.png

image.png

public static void test(int a) {
    if (5 <= a && a <= 10 && a != 8) {
        System.out.println("输出");
    }
}

@Test public void test() {
    StringMethod.test(4);
}
@Test public void test1() {
    StringMethod.test(5);
}

image.png

image.png

public static void test(int a) {
    if (5 <= a && a <= 10 && a != 8) {
        System.out.println("输出");
    }
}

@Test public void test() {
    StringMethod.test(4);
}
@Test public void test1() {
    StringMethod.test(5);
}
@Test public void test2() {
    StringMethod.test(8);
}

image.png

image.png

public static void test(int a) {
    if (5 <= a && a <= 10 && a != 8) {
        System.out.println("输出");
    }
}

@Test public void test() {
    StringMethod.test(4);
}
@Test public void test1() {
    StringMethod.test(5);
}
@Test public void test2() {
    StringMethod.test(8);
}
@Test public void test11() {
    StringMethod.test(11);
}

image.png

image.png

4.5、 if-else if-else

if (a <= 5) {
    System.out.println("输出");
    System.out.println("输出");
    System.out.println("输出");
} else if (a <= 10) {
    System.out.println("输出1");
    System.out.println("输出1");
    System.out.println("输出1");
} else {
    System.out.println("输出2");
    System.out.println("输出2");
    System.out.println("输出2");
}


@Test public void test() {
    StringMethod.test(4);
}

image.png

image.png

if (a <= 5) {
    System.out.println("输出");
    System.out.println("输出");
    System.out.println("输出");
} else if (a <= 10) {
    System.out.println("输出1");
    System.out.println("输出1");
    System.out.println("输出1");
} else {
    System.out.println("输出2");
    System.out.println("输出2");
    System.out.println("输出2");
}

@Test public void test() {
    StringMethod.test(4);
}
@Test public void test1() {
    StringMethod.test(6);
}

image.png

image.png

if (a <= 5) {
    System.out.println("输出");
    System.out.println("输出");
    System.out.println("输出");
} else if (a <= 10) {
    System.out.println("输出1");
    System.out.println("输出1");
    System.out.println("输出1");
} else {
    System.out.println("输出2");
    System.out.println("输出2");
    System.out.println("输出2");
}

@Test public void test() {
    StringMethod.test(4);
}
@Test public void test1() {
    StringMethod.test(6);
}
@Test public void test2() {
    StringMethod.test(11);
}

image.png

image.png

总结:

1、两者都是只有If 才算分支、else 不算

2、JaCoCo 直接统计、2个if算4个分支

3、IDEA 动态的、if感觉只会统计走到的?

4.6、多个方法时候

public static void test(int a) {
    if (a <= 5) {
        System.out.println("输出");
        System.out.println("输出");
        System.out.println("输出");
    } else if (a <= 10) {
        System.out.println("输出1");
        System.out.println("输出1");
        System.out.println("输出1");
    } else {
        System.out.println("输出2");
        System.out.println("输出2");
        System.out.println("输出2");
    }
}

public static void test2(int a) {
    if (a <= 5) {
        System.out.println("输出");
        System.out.println("输出");
        System.out.println("输出");
    } else if (a <= 10) {
        System.out.println("输出1");
        System.out.println("输出1");
        System.out.println("输出1");
    } else {
        System.out.println("输出2");
        System.out.println("输出2");
        System.out.println("输出2");
    }
}

@Test public void test() {
    StringMethod.test(4);
}

image.png

image.png

总结:

1、IDEA 真的就是 只统计走到的、然后多少全覆盖了、a/b代表已经涉及了b个判断条件、a个全覆盖了

2、JaCoCo 统计实际存在的、和走没走到没关系

3、都是值统计If、不统计else

4、JaCoCo算方法最后的大括号、IDEA不算、他们都不算if判断条件结束的那个大括号


IDEA 覆盖率覆盖颜色配置---我习惯的配置

image.png

image.png

image.png