如何在IntelliJ IDEA中获取实际的代码覆盖率报告?

StackExchange123:

我有一个简单的类,叫做SomeClass

public class SomeClass {

    public int value = 0;

    public void inc() {
        value++;
    }

    public void dec()
    {
        if (isBiggerThanFive()) value--;
        value--;
    }

    private boolean isBiggerThanFive() {
        return value > 5;
    }
}

还有一个测试类TheTest

class TheTest {

    SomeClass t;

    @BeforeEach
    void setUp() {
        t = new SomeClass();
    }

    @Test
    public void whenNewTestIsCreated_ValueIsZero()
    {
        assertEquals(t.value, 0);
    }

    @Test
    public void whenIncIsCalledWithValueOfZero_ValueIsOne()
    {
        t.inc();
        assertEquals(t.value, 1);
    }

    @Test
    public void whenDecIsCalledWithValueOfZero_ValueIsNegativeOne()
    {
        t.dec();
        assertEquals(t.value, -1);
    }
}

注意函数dec的结构以及整个if条件和语句如何在同一行。在测试中,当值大于5时,这部分递减2而不进行测试。因此,我认为我应该在覆盖率报告中看到这一部分没有涉及。但这就是我得到的:

相反,当我让陈述独立存在时,我会得到正确的结果:

Even though the code is completely the same, I get different results based on just the structure of the code. How can I have an indication that a part of a line isn't covered when it's not acutally covered?

Ivo Mori :

IntelliJ's test coverage runner has different run settings (for performance reasons). By default, the Sampling mode is used. This results in a code line coverage with negligible (execution) slow-down. IntelliJ Test Coverage Samling setting Now, if you'd like to have an accurate collection of the branch coverage (such as if-statements) then you can use the Trace mode. IntelliJ Test Coverage Trace setting Using the Trace mode you'll get the following test coverage.

Trace coverage report

Trace coverage hits

This is further documented on IntelliJ's help website. See Configure code coverage options.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

代码覆盖率的IntelliJ

如何从EclEmma中的覆盖率计算中排除类而不实际从覆盖率本身中排除类

测试pytest插件时如何获取覆盖率报告?

IntelliJ IDEA:在代码覆盖率中忽略琐碎的方法

如何在IntelliJ IDEA中从覆盖率测量中排除源代码?

如何让Emma或Cobertura与Maven一起报告其他模块中源代码的覆盖率?

如何在Golang中获得100%的代码覆盖率?

如何显示Flutter测试中的代码覆盖率数据?

如何衡量Golang中的代码覆盖率?

如何在Xcode中隐藏代码覆盖率注释?

如何在Intellij中查看代码覆盖率的详细信息

如何为Android单元测试生成代码覆盖率报告

如何在Clion中查看代码覆盖率

如何过滤llvm-cov代码覆盖率报告中的文件?

如何在VSTS中合并代码覆盖率结果

如何在Android Studio 2.3中配置代码覆盖率报告?

如何从Cake获取TeamCity中的DotCover覆盖率

如何合并覆盖率报告?

如何为pytest执行的SWIG编译的C代码生成代码覆盖率报告

如何获取蔚蓝管道的代码覆盖率徽章

尽管覆盖率报告为100%,如何查找从未在coverage.py中执行的代码

如何获取类似于Sonarqube的Salesforce代码覆盖率报告

如何获取增量代码的代码覆盖率并将其作为注释发布到git PR中

如何在单个HTML文件中获取Django覆盖率报告

如何获取外部API的覆盖率报告?

如何添加注释以从 jacoco 代码覆盖率报告中排除方法?

如何在 Sonarqube 中获得新的代码覆盖率?

如何获取代码覆盖率信息?

如何在 Swift 包中显示代码覆盖率?