Main Content

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万博1manbetx®Test™Test Manager orsltest.testmanager.CoverageSettings, you must haveSimulink Coverage™installed. Although you can set up and run test cases using onlySimulink 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 haveSimulink Requirements™installed, you can also use the Test Manager to verify that coverage results are traced to specific requirements.

Note

Coverage is not supported for Software-in-the-Loop (SIL) or Processor-in-the-Loop (PIL) blocks.

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 theCoverage Settingssection. UnderCoverage to Collect, selectRecord coverage for system under testto turn on coverage collection for the model specified as theSystem Under Testin each test case. SelectRecord coverage for referenced modelsto collect coverage for models that referenced from within the specified system under test. The selected coverage settings propagate from the test file to the test suites and test cases in the test file.

    Coverage settings with record coverage for system under test selected

  3. Optionally, to add or remove existing coverage filter files, clickAddorRemove, 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, seeTypes of Model Coverage(Simulink Coverage)andModel Objects That Receive Coverage(Simulink Coverage).

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

To remove individual test suites or test cases from collecting coverage data, select the test suite or test 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 theResults and Artifactspane in the Test Manager to view the results. Coverage results are reported in results sets. Select aResultsitem in the pane and expand theAggregated Coverage Resultssection. 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 theResults and Artifactslist. Then, from the context menu, selectMerge 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, enableScope coverage results to linked requirementsto check that your model design is executing the requirements and that the tests are verifying those requirements. BothSimulink CoverageandSimulink Requirementslicenses are required. This option is available only if the results set contains more than one simulation, such as multiple test cases or iterations.

When theScope coverage results to linked requirementscheck 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 theCoverage Detailspane.

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报告column of theAggregated Coverage Resultstable.

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, which are shown for a result set, update automatically. You do not have to resimulate your model. For more information, seeCoverage Filtering(Simulink Coverage).

To view the filtered coverage results, select a result set (that is, aResultsitem) in theResults and Artifactspane.

From the Test Manager, you can:

  • Add or remove an existing coverage filter file — In theTest Browserpane, select the test file and expand theCoverage Settingssection. ClickAddorRemove底部的Coverage FiltersorApplied Coverage Filterstable 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 aCoverage Reportor theCoverage Detailspane of a model, open theSimulink CoverageFilter Editor by clicking on a justify iconor aJustify or Excludelink. 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, seeCreating and Using Coverage Filters(Simulink Coverage)andCreate, Edit, and View Coverage Filter Rules(Simulink Coverage).

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

  • View coverage results — Select aResultsitem in theResults and Artifactspane and expand theAggregated Coverage Resultssection.

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

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

See Also

Related Topics