Collect Coverage in Tests

Coverage refers to determining the testing completeness of models and generated code by analyzing how much of the model has been exercised. To collect coverage using the Simulink® Test™ Test Manager or sltest.testmanager.CoverageSettings, a Simulink Coverage™ license is required. Although you can set up and run test cases using only Simulink Coverage, Simulink Test provides additional test creation and test management features. For tests with coverage collection turned on, the Test Manager includes the coverage of each metric you choose to collect in the results. If you have a license for Simulink Requirements™, you can also use the Test Manager to verify that coverage results are traced to specific requirements.

Set Up Coverage Collection Using the Test Manager

In the Test Manager, you can enable coverage and select the coverage metrics at the test file level. Test suites and test cases inherit the coverage settings from the test file. However, you can turn off coverage collection for individual test suites and test cases.

To set up the Test Manager to include coverage collection:

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

  2. Select the test file and expand the Coverage Settings section. Under Coverage to Collect, select Record coverage for system under test to turn on coverage collection. You can also collect coverage for referenced models.

    Coverage settings with record coverage for system under test selected

  3. Optionally, to add or remove existing coverage filter files, click Add or Remove, respectively, in the Coverage Filters section and select the filter file. More than one filter file can be applied at the same time.

  4. Select the coverage metrics to collect. For information on metrics, see Types of Model Coverage (Simulink Coverage) and Model Objects That Receive Coverage (Simulink Coverage).

  5. Run the test. Coverage is collected for all test suites and test cases in the test file.

To remove individual test suites or test cases from collecting coverage data, select the suite or case and change its coverage settings.

View and Filter Coverage Results in the Test Manager

View Aggregated Coverage Results and Metrics

After you collect coverage, use the Results and Artifacts pane in the Test Manager to view the results. Coverage results are reported in results sets. Select a Results item in the pane and expand the Aggregated Coverage Results section. The coverage percentage is shown for each metric and the colors summarize the coverage results.

  • Dark blue — Satisfied coverage

  • Red — Unsatisfied coverage

  • Light blue — Justified coverage

Summary and aggregated coverage results for a Result Set in the Test Manager.

To aggregate results from different test files into a single Result Set, select the separate results in the Results and Artifacts list. Then, from the context menu, select Merge Coverage Results. A Results Set that contains the combined coverage results appears in the list.

Scoping Coverage for Requirements-Based Tests

For requirements-based design and testing, such as for compliance to DO-178B, enable Scope coverage results to linked requirements to check that your model design is executing the requirements and that the tests are verifying those requirements. Both Simulink Coverage and Simulink Requirements licenses are required. This option is available only if the results set contains more than one simulation, such as multiple test cases or iterations.

When the Scope coverage results to linked requirements check box is selected, coverage results include only tests that are directly linked to requirements and are explicitly tested. The aggregated results update automatically without having to resimulate the model. If you have tests that touch a model component but are not directly linked to a requirement, your aggregated coverage results percentages might decrease when you enable scoping. To obtain 100% coverage to your requirements, you might need to update your tests, add requirements links, or justify or exclude some items from coverage.

Trace Coverage Results to the Model

To navigate from the test coverage results in the Test Manager to the model, click the model name in the Aggregated Coverage Results table.

The model opens, and its Coverage Report opens in the Coverage Details pane of the model window. In this sample model, the model elements are red because they have less than 100% coverage.

Coverage report with highlighted model elements.

Point to a model element to see a summary of its metrics and block execution.

Hover pop up showing decision at 50% and execution at 100% coverage

Click a model element to scroll to its detailed coverage results information in the Coverage Details pane.

Coverage details with model element highlights

Create a Coverage Report

To create a report of the coverage for a model, click the arrow in the Report column of the Aggregated Coverage Results table.

Coverage Filtering Using the Test Manager

Coverage filter rules specify one or more model objects or lines of generated code to exclude from coverage collection or for which you want to justify the coverage results. A set of coverage filter rules is contained in a filter file, which can be applied to the model or code being tested. You can apply more than one filter file to a test and also, reuse filter files for different models. When you apply a new or updated filter, the aggregated coverage results update automatically. You do not have to resimulate your model. For more information, see Coverage Filtering (Simulink Coverage).

From the Test Manager, you can:

  • Add or remove an existing coverage filter file — Click Add or Remove at the bottom of the Coverage Filters or Applied Coverage Filters table and select the coverage filter file to add or remove, respectively. More than one coverage filter file can be applied to the coverage results.

  • Edit or create a filter file, define a filter rule, and justify or exclude coverage — From a Coverage Report or the Coverage Details pane of a model, open the Simulink Coverage Filter Editor by clicking on a justify icon or a Justify or Exclude link. When the Filter Editor is open, the Test Manager is locked. When you close the Filter Editor, the Test Manager is enabled and the results and applied filters list are updated with your changes. For information on using the Filter Editor, see Creating and Using Coverage Filters (Simulink Coverage) and Create, Edit, and View Coverage Filter Rules (Simulink Coverage).

  • Append currently applied coverage filters to the test file — Click Update Test File.

For more information on coverage filters, rules, and files, see the Coverage Filtering topics in Evaluate Coverage Results (Simulink Coverage).

For information on considerations when collecting coverage in a test harness, see Test Harness Considerations in Test Harness and Model Relationship.

See Also

Related Topics