Contenuto principale

Rules for Propagating Code Coverage Justifications Across Multiple Results

You can calculate code coverage metrics to see how much of your C/C++ code is validated by existing tests. To address gaps in code coverage, you can choose to add more tests or justify the missing code coverage. For more information on justifications, see Improve or Justify Missing Code Coverage Results.

When justifying coverage results, you might add the justification to a specific result but see one or more additional coverage results justified. This topic lists some of the rules for how justifications propagate across coverage results.

Justifications Propagate Downwards

If you justify a parent element, the justification propagates to all child elements that are missing coverage. For instance:

  • If you justify missing code coverage at file level, all missing coverage (decisions, conditions, MC/DC) in the file gets justified.

  • If you justify missing code coverage at function level, all missing coverage in the function gets justified.

  • If you justify missing coverage of a decision, missing coverage for all conditions that are part of the decision gets justified.

You can apply justifications to a parent element in one of the following ways:

Justifications Also Propagate Upwards

If you justify a child element and this child element is the only one among its siblings that is missing coverage, the justification propagates upwards to parent elements.

For instance, if you justify the missing coverage of a decision outcome and this outcome is the only outcome of the decision that is missing coverage, then the parent decision is justified. Likewise, if this decision happens to be the only decision with missing coverage in a function, then the missing decision coverage of the function is justified.

Justifying Missing Decision Outcome Justifies Downstream Decisions

If you justify a missing decision outcome, you are effectively signaling that all decisions that are downstream of this outcome do not require coverage. Therefore, any missing coverage of the downstream decisions are also justified.

For instance, consider this example:

#include <stdbool.h>

int checkConditions (bool cond1, bool cond2)
{
    if(cond1 && cond2)
        {
            return 1;
        }
    else if(cond1 || cond2)
        {
            return 2;
        }
    else
        {
            return 3;
        }
}
Suppose you write a test that invokes checkConditions() with the inputs: cond1 = 1, cond2 = 1. This test only exercises the true outcome of the decision cond1 && cond2 and does not exercise the other decision cond1 || cond2 at all. In your coverage results, if you justify the missing coverage of the false outcome of cond1 && cond2, the missing coverage of the other decision is also justified.

See Also

Topics