我有一个简单的类,叫做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?
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. 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. Using the Trace
mode you'll get the following test coverage.
This is further documented on IntelliJ's help website. See Configure code coverage options.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句