nus-cs2103-AY2223S2 / forum

12 stars 0 forks source link

Branch coverage vs path coverage vs statement coverage #424

Closed BoAi01 closed 1 year ago

BoAi01 commented 1 year ago

I refer to this article for the comparison between these three kinds of coverages, which mentions that

Path Testing >= Branch Testing >= Statement Testing

Based on the textbook, we shall agree that Path Testing >= any other testing, but why Branch Testing >= Statement Testing?

Screenshot 2023-04-25 at 4 27 36 PM

More specifically I am considering a simple example to check my basic understanding:

if (a > 0) {
    b = a + 1;
} else {
    b = a - 1;
}

If we have one test case a = 1, and the if block is executed. What are the branch coverage rate and statement coverage rate? I think these should be 50% and 50% respectively.

BoAi01 commented 1 year ago

I actually feel that if every statement is covered, then every branch is covered. Any counter-examples?

SPWwj commented 1 year ago

In this case, if the test cases only execute the scenario where the condition is true, all the statements (statement1 and statement2) will be executed, resulting in 100% statement coverage. However, branch coverage will still be 50%, as the scenario where the condition is false has not been tested.

if (condition) {
    statement1;
}
statement2;
BoAi01 commented 1 year ago

I see. Then this makes sense now. Thanks

Path Testing >= Branch Testing >= Statement Testing

damithc commented 1 year ago

Good example, @SPWwj