主要内容

Merge Coverage Results from Simulink Coverage and Polyspace Test

Your code base can include MATLAB® code, C/C++ code generated from Simulink® models, and hand-written C/C++ code. You can gather code coverage of these components and then review all coverage results together in the Polyspace Platform user interface. In this way, you can track the coverage of both generated code and hand-written code in the same dashboard.

This example shows how to gather code coverage of generated code by using Simulink Coverage™, gather code coverage of hand-written code using Polyspace® Test™, and then merge the results and review them together in Polyspace Platform user interface.

Prerequisites

Following this example requires product licenses for:

  • Simulink Coverage

  • Embedded Coder®

  • Polyspace Test

The source files required for this example are located in the folder polyspaceroot\polyspace\examples\doc_pstest\coverage_data_collection, where polyspaceroot is the Polyspace Test installation folder, for instance, C:\Program Files\Polyspace\R2026a. Copy the content of this folder to a writable folder.

Gather Code Coverage of Generated Code

Open the model slvnvdemo_counter_harness in a writable location.

openExample('slcoverage/SoftwareintheLoopCodeCoverageExample')
This model is a test harness for the referenced model slvnvdemo_counter. The Signal Editor block Inputs provides inputs corresponding to a test case. To generate code and collect code coverage of slvnvdemo_counter corresponding to this test case :

  1. Select the Model block slvnvdemo_counter. In the Model Block tab, set Simulation Mode to Software-in-the-Loop (SIL). The corners of the block icon turn black and (SIL) appears under the model name on the block.

  2. Open the Configuration Parameters dialog box. In the Coverage pane, set these parameter:

    • From the Structural coverage level list, select Modified Condition Decision Coverage (MCDC).

    • Select Save last run in workspace variable.

    • Click OK to close the dialog box.

    Configuration Parameters dialog box. Structural coverage level is set to Modified Condition Decision Coverage (MCDC) and Save last run in workspace variable is selected.

  3. In the Apps tab, open the Coverage Analyzer app. Then, on the Coverage tab, click Analyze Coverage. Simulink Coverage generates the source file slvnvdemo_counter.c for the model, calculates the coverage of the source file for the test case, and saves the coverage results as the object covdata in the MATLAB workspace.

  4. Export the coverage results into Polyspace Test format.

    pstest.exportToPolyspaceProfilingResults(covdata)
    The coverage results are exported in the file output.psprof. Note the complete path of this file for later use.

You can open this file in Polyspace Platform user interface and observe that the generated code has 100% coverage for the test case.

For more details about calculating coverage of generated code, see Code Coverage for Models in Software-in-the-Loop (SIL) Mode and Processor-in-the-Loop (PIL) Mode (Simulink Coverage).

Gather Code Coverage of Hand-Written Code

To calculate coverage for hand-written code:

  1. In the Polyspace Platform user interface, create a new project and add the source files in the folder coverage_data_collection/src.

  2. Click Parse Code to find the functions foo(), bar(), and multiply() in the source files.

  3. Add test cases for foo(), bar(), and multiply() by right-clicking a function and selecting Add Test Case. Add these test cases:

    • foo(2,0)==2

    • bar(1,1,0)==0

    • bar(1,1,1)==1

    • bar(1,0,1)==1

    • bar(0,1,1)==1

    • multiply(0,0)==0

    • multiply(2,2)==4

  4. In the Profiling section of the Polyspace Platform toolstrip, select Code Coverage.

  5. Double click the Configuration node in the Projects pane. In the Testing & Profiling tab, select the Code Coverage and then from the Coverage metrics list, select MCDC.

  6. In the Project tab, click Build Project and then click Run Tests.

Once the tests finish running, the test results and coverage results appear in the Results pane. To locate the .psprof file containing the coverage results, right-click the Coverage results and then select Show in Explorer. Note the complete path to the file ps_coverage.psprof for later use.

Merge and Review Coverage

To merge the coverage of the generated code and the hand-written code, in the system command line, enter:

polyspace-code-profiler -merge -results-dir <merged_results> -operation aggregate-variants <path_to_output.psprof> <path_to_ps_coverage.psprof>
In this command, use these folder paths:

  • merged_results — The folder where the merged .psprof file is stored.

  • path_to_output.psprof — The complete path to the output.psprof file containing the coverage of generated code.

  • path_to_ps_coverage.psprof — The complete path to the ps_coverage.psprof file containing the coverage of the hand-written code.

To review the merged result, open the merged .psprof file in the folder merged_results using the Polyspace Platform user interface.

Merged results showing coverage for generated and hand-written code

You can review the coverage of both generated code and handwritten code in the same interface.

See Also

|

Topics