Collect Coverage in Tests

If you use Simulink® Coverage™ to generate model and code coverage, then you can collect coverage metrics when you run your test cases. For test cases with coverage collection turned on, Test Manager includes the coverage of each metric you specify to collect in the results.

To test a model for coverage, turn on coverage collection on the test file and specify the metrics you want to collect. Test suites and test cases inherit the settings from the test file. After coverage is turned on at the file level, you can turn it off and on for each test suite or test case.

Enable and Collect Coverage for a Test File

Enable coverage collection, view coverage results in Test Manager, and trace coverage results from Test Manager to the model. The model sldemo_autotrans specifies coverage.

For information about types of model coverage, see Types of Model Coverage (Simulink Coverage).

  1. Create a test file and set up the test case for your model.

  2. In the test file settings, under Coverage Settings, select Record coverage for system under test. You can also specify whether to collect coverage for referenced models.

  3. Select the coverage metrics that you want to collect.

  4. Run the test file.

  5. To view the coverage results, select the test case result in the Results and Artifacts pane and expand the Coverage Results section.

  6. If your test file or test suite collected coverage metrics for more than one model, you can view all the coverage metrics in one place. Select the result and expand the Aggregated Coverage Results section.

  7. To trace the coverage results to the model, click the model name in the coverage results table.

    In the model, select model elements to see the coverage data.

  8. To create a report of the coverage for a model, click the arrow under the Report column in the coverage results.


To see aggregated results from different test files, in the Results and Artifacts pane, select the test file results whose coverage results you want to see in the same results file. From the context menu, select Merge Coverage Results. A results file that contains the combined coverage results appears in the list.

Considerations for Collecting Coverage in Test Harnesses

Loading coverage results to a model, or aggregating coverage results across models, requires a model consistent with the coverage results. Therefore, to perform aggregated coverage collection, it is recommended that you use test harnesses configured to automatically synchronize the component under test. Set SynchronizationMode to Synchronize on harness open and close. For more information, see Synchronize Changes Between Test Harness and Model.

Coverage results association depends on test harness – main model synchronization:

  • If the test harness is configured to synchronize the component under test when you open or close the harness, coverage results from the test harness are associated with the main model. When you close the test harness, the coverage results remain active in memory. You can aggregate coverage with additional results collected from the main model or another synchronized test harness.

  • If the test harness is configured to only synchronize the component under test when you manually push or rebuild, the coverage results are associated with the test harness.

    • When you close the test harness, the coverage results are removed from memory.

    • If the component under test design differs between test harness and main model, you cannot aggregate coverage results.

    • You can aggregate coverage results with the main model if the component under test design does not differ, but you must manually load the coverage results into the main model. See the function cvload.

See Also

Related Topics