Testing - Exercise Sheet 8.3 - Statements and Branches

Here is Exercise Sheet 8.3:
Exercise 3.: Coverage

abafa93484010e0fa78fcfcae35b2d9a44516b98

I would like to ask what does “covering all statements” and “covering all branches” mean and the difference between those.
I could come up with the solution but I don’t what those expressions really mean.

Thanks.

Subsection 7.3.3: White-box Testing and Coverage

  • Definition 7.3.4: Statement Coverage
    Testing
  • Definition 7.3.5: Branch Coverage
    Testing
1 Like

Thank you for the message. I read it but I could not understand properly.

If we want to express informally those definitions; how would it be?

I could express “Statement Coverage” as follows; line being executed at the test case. That is to say, if we are writing a test case for covering all statements, the test suite we write should execute every line in the function we are testing. (I might be mistaken, would be glad if there is a better explanation)

But I could not come up with an informal expression of “Branch Coverage”. What does covering all branches and particularly branch mean? I would be glad if you can help me with that.

Thanks

Yes, you want to cover all statements (and nested statements).
For instance, full statement coverage would need to also cover line 3 in Remark 7.3.6.

Usually, it is not possible to achieve full statement coverage with only one test case. As you probably noticed in your Java projects, you need many test cases to cover all possibilities that lead to different statements.

At certain instructions, the control flow of programs can take a branch.
For instance, for conditionals, you can either take the consequence or alternative.
You are usually interested in covering the possible cases of your program.
Therefore, you want a full branch coverage that guarantees that you took each branch (at least once the consequence and once the alternative).

If you covered all branches, you also covered all statements in these branches. Therefore, branch coverage implies statement coverage.

And then, there is an even stronger coverage:
Definition 7.3.7: Path Coverage

1 Like

What would be an example of having all statement coverage while not having all branch coverage - if its possible ?

Particularly, I guess, at the example above, we cannot extend the test suite for covering all branches since when we do the a part, we basically get done for the second part. Because, there is no alternative (“else part”) for statement else if (x == 2) . We just continue from there with return statement but apparently it is not the case for the solution.

The lecture notes provide one:

Test suite = Set of Test Cases
Test Case = One input and the resulting execution

A test suite can reach full statement and branch coverage in the program from the exercise.