主要内容

Compare Code Coverage Results After Adding New xUnit API-Based Tests

Code coverage results help you assess if your test cases test the execution paths in your code sufficiently. After reviewing the code coverage metrics, you can add new tests to cover previously uncovered execution paths. After adding new tests:

  • Isolate the new results caused by your added tests. This finding helps you understand if the added tests improve the coverage of your code.

  • Find results that are common between your new tests and previous tests. This finding helps you understand if the added tests interfere with preexisting tests.

  • Obtain one merged set of results. This finding is useful for recording the results from the entirety of your test suite.

You can compare results between graphically created test cases by using the Polyspace® Platform user interface. This example shows how to compare results between tests authored by using the Polyspace Test™ xUnit API.

Prerequisites

Before you compare code coverage results, you must be able to:

To follow this topic:

  1. Copy this code into a *.c file:

    int foo(int A, int B, int C){
        if(A||B&&C)
            return 1;
        return 0;
    }
    

  2. Calculate the code coverage of these test cases:

    • foo(0,0,1)==0

    • foo(0,1,1)==1

    Store the results (*.psprof) file in the folder run1.

  3. Calculate the code coverage of these new test cases:

    • foo(0,0,1)==0

    • foo(0,1,1)==1

    • foo(0,1,0)==0

    • foo(1,0,1)==1

    Store the new results (*.psprof) file in the folder run2.

.

Find Difference in Coverage Result After Adding New Tests

To identify the impact of the new added tests, calculate the difference between the new code coverage results and the old code coverage results.

  1. Calculate the difference between the results in run1 and run2:

    polyspace-code-profiler -merge -operation diff -results-dir opDir run2 run1
    This command calculates the difference between run2 and run1. The diff operation produces different results depending on the order of the input folders. Polyspace stores the calculated difference in the folder opDir as a *.psprof file.

  2. Generate a report from the difference by entering this command, which stores the report in opRep:

    polyspace-code-profiler -report -html -report-dir opRep opDir

After reviewing the report, you see that the new tests increase condition coverage by 33% and MCDC coverage by 33%.

Find Common Results

Identify which code coverage results are common to both new and additional set of tests. Calculate the intersection between the sets of results:

  1. Calculate the intersection between the results in run1 and run2:

    polyspace-code-profiler -merge -operation intersection -results-dir opDir run2 run1
    Here, opDir is the folder where the intersection is stored as a *.psprof file.

  2. Generate a report from the intersection by entering this command, which stores the report in oprRep.

    polyspace-code-profiler -report -html -report-dir opRep opDir

After you review the report, you see that the code coverage in run1 also exists in run2. That is, the added tests do not interfere with the preexisting tests.

Find Total Coverage

To see the total code coverage of all the tests, calculate the union of the coverage results:

  1. Calculate the union between the results in run1 and run2:

    polyspace-code-profiler -merge -operation union -results-dir opDir run2 run1
    Here, opDir is the folder where the union is stored as a *.psprof file.

  2. Generate a report from the union by entering this command, which stores the report in opRep.

    polyspace-code-profiler -report -html -report-dir opRep opDir

See Also

Topics