Criteria for calculating code coverage in unit test

Tram Ho

In unit tests, code coverage is a value that represents the coverage of the code by the test cases expressed as%, it shows the proportion of code that was executed by the unit test above. The total number of code for a program. Code coverage is calculated using the following formula

items in the above formula are not just the number of lines of code but also other objects such as statements, branches, conditions, …

Statement Coverage

Statement coverage shows the number of statements of the program that were executed during the test, we can roughly understand statement coverage is the coverage of the lines, i.e. the number of lines of code. was run through the test. A command line is counted as executed when only one or all of its code is executed, so statement coverage does not guarantee that your program is 100% error free. We can see the following example:

For example

In the above example we can easily get 100% statement coverage by testing with input foo (1), then the if statement will be executed, but if the input is 0 then in the return line will cause a division by zero error

Evaluation of statement coverage

As seen in the above example, even if the statement coverage is 100%, the program still has a lot of critical errors, so the command line coverage only shows that all lines of code have been executed. Test at least once. In terms of quality, a program with 100% statement coverage is less likely to fail than a 70% program but covers more cases. However, statement coverage is very useful in dead code detection, ie all lines of program code are run and compile-free. Statement coverage can also reveal missing test cases by indicating portions of code that have not been tested.

Branch Coverage

Branch coverage targets all branches of the flow of a piece of code / function. The simplest example of a branch is an if conditional structure that will have two branches, the then branch and the else branch

An if condition always has 2 branches

With the if condition, there are some cases which are not written in code, but the else branch always exists. As in the example in section 1, we will perform the same test as in part 1 for this code using the figure below but use the branch coverage measure instead of statement coverage

For ease of visualization see the block diagram below

The picture above shows the test method to achieve 100% statement coverage in item 1, the else branch hidden has not been executed (highlighted in red), so we need to add a second test case to be able to achieve it. With 100% branch coverage, with a second test case simply foo (0), we can easily detect the error in the hidden branch.

Condition Coverage

Usually a condition in if can be combined from many different conditions through logical operators (AND, OR, NOT). A condition is a boolean expression that does not contain a logical operator, branch coverage reaches 100%, it is unlikely that the conditional coverage will reach 100%, I will explain in the following example:

With branch coverage, just passing one of the two conditions decision1 or decision2 under the if condition will be counted as covering this if branch, we can easily reach 100% branch with the following test cases:

  • foo (1, 0): branch1 covered
  • foo (0, 0): branch2 covered However with the conditional coverage technique, decision1 and decision2 are counted as two independent conditions so the above test cases will not be enough to cover all the conditions. The desition2 condition is wrong, we can’t see the bug, we have to add at least one test case to achieve 100% conditional coverage:
  • foo (1, 0): branch1 covered, decision1 covered
  • foo (0, 1): branch1 covered, decision2 covered
  • foo (0, 0): branch2 covered

Conclusion

Hope the above article will give everyone a quick understanding of the concept of code coverage, thank you for taking the time to follow.

Share the news now

Source : Viblo