New Code Coverage Calculation for Multiline Statements

Code coverage has changed for statements that are written on multiple lines. Each line that contains an expression is now included in the code coverage calculation for covered and uncovered code. Previously, a multiline statement was calculated as one line of code in code coverage. This change affects all API versions.

As a result of this change, code coverage numbers can sometimes be calculated differently from the calculation done in Summer ’15. The new calculation can be higher or lower, depending on the scenario. When multiline statements are present, more lines are counted as part of the code coverage calculation. Therefore, if those lines are covered, they can contribute to a higher coverage. Similarly, if those lines aren’t covered, they can contribute to a lower coverage.

This example shows how code coverage can be higher. The following code snippet results in six lines that are counted toward code coverage. Those lines are Integer n = getSomeNumber1(), + getSomeNumber2(), + getSomeNumber3(), + getSomeNumber4();, if (myVar==true) {, and notCovered();. In this example, only the notCovered(); statement isn’t covered, so the total number of covered lines is five. The code coverage is 5/6 or 83%.

Code example shows higher code coverage in Summer '15

Previously, three lines of code were counted, because all the expressions in the first assignment statement (Integer n =) were counted as one line. The code coverage was 2/3 or 66%.

Code example shows lower coverage in Summer '15

This next example shows how code coverage can be lower. The expressions in the if condition result in three lines that are counted toward code coverage. Because fooReturnsFalse() returns a false value, the remaining expressions in the condition aren’t evaluated, and therefore aren’t covered. The code coverage is 1/3 or 33%.

Code example shows lower coverage in Summer '15

Previously, all three expressions in the if condition were counted as one line for code coverage, and the code coverage was 1/1 or 100%.

Code example shows higher coverage in Summer '15
Note

Note

  • If a statement consists of multiple expressions and is written on one line, it’s counted as one line for code coverage. If one part of this statement is covered and other parts aren’t, the entire statement is counted as a covered line because it counts as one line total.
  • The Apex compiler sometimes optimizes expressions in a statement. For example, if multiple string constants are concatenated with the + operator, the compiler replaces those expressions with one string constant internally. If the string concatenation expressions are on separate lines, the additional lines aren’t counted as part of the code coverage calculation after optimization. To illustrate this point, a string variable is assigned to two string constants that are concatenated. The second string constant is on a separate line.
    String s = 'Hello'
        + ' World!';
    The compiler optimizes the string concatenation and represents the string as one string constant internally. The second line in this example is ignored for code coverage.
    String s = 'Hello World!';