Compare Code Coverage Results After Adding New xUnit API-Based Tests
R2026bCode 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:
Author tests or test suites using the xUnit API. See Write C/C++ Unit Tests Using Polyspace Test xUnit API and Run Tests at Command Line.
Calculate code coverage of xUnit based tests. See Calculate C/C++ Code Coverage Using Self-Managed Builds.
To follow this topic:
Copy this code into a
*.cfile:int foo(int A, int B, int C){ if(A||B&&C) return 1; return 0; }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 folderrun1.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 folderrun2.
.
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.
Calculate the difference between the results in
run1andrun2:This command calculates the difference betweenpolyspace-code-profiler -merge -operation diff -results-dir <opDir> run2 run1run2andrun1. Thediffoperation produces different results depending on the order of the input folders. Polyspace stores the calculated difference in the folder <opDir> as a*.psproffile.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:
Calculate the intersection between the results in
run1andrun2:Here, <opDir> is the folder where the intersection is stored as apolyspace-code-profiler -merge -operation intersection -results-dir <opDir> run2 run1*.psproffile.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:
Calculate the union between the results in
run1andrun2:Here, <opDir> is the folder where the union is stored as apolyspace-code-profiler -merge -operation union -results-dir <opDir> run2 run1*.psproffile.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>