Main Content

sltest.plugins.ModelCoveragePlugin class

Package: sltest.plugins

Collect model coverage using the MATLAB Unit Test framework

Description

The sltest.plugins.ModelCoveragePlugin creates coverage reports and allows setting coverage metrics for running Simulink® Test™ and MATLAB®-based Simulink test cases with the MATLAB Unit Test framework. Set desired sltest.plugins.ModelCoveragePlugin property values, and add the instance of the sltest.plugins.ModelCoveragePlugin to the test runner. For MATLAB-based Simulink tests, calls to the simulate method collect coverage during the test run. These coverage results are available in the Test Manager results. If you have a license for Parallel Computing Toolbox™, you can use the ModelCoveragePlugin with parallel test execution.

Creation

mcp = sltest.plugins.ModelCoveragePlugin(Properties) creates a model coverage plugin object mcp with specified properties.

You can also import the plugin, then use the class name to create an instance of the plugin:

import sltest.plugins.ModelCoveragePlugin
mcp = ModelCoveragePlugin(Properties)

Properties

expand all

Property that disables or enables coverage collection for models referenced by Model blocks.

Example: 'RecordModelReferenceCoverage',true

Attributes:

SetAccess
public
GetAccess
public

Property that specifies coverage collection options with a sltest.plugins.coverage.CoverageMetrics object. If you enable Condition coverage, the Coverage Report shows both Condition and Decision coverage. If you enable MCDC, Condition and Decision coverage are also collected and are included in the Coverage Report.

Example: 'Collecting',covSettings

Example: 'Collecting',CoverageMetrics('MCDC',true,'Decision',true,'Condition'true)

Attributes:

SetAccess
public
GetAccess
public

Property that specifies coverage report options with an sltest.plugins.coverage.ModelCoverageReport.

Example: 'Producing',mcr

Example: 'Producing',ModelCoverageReport('reports/coverage/modelcoverage')

Attributes:

SetAccess
public
GetAccess
public

Examples

collapse all

This example shows how to use MATLAB® Unit Test to collect coverage for tests run on a Simulink® model.

You run the tests in the AutopilotTestFile.mldatx test file while collecting modified condition/decision (MCDC) coverage.

1. Import the test runner and the plugins for the example.

import matlab.unittest.TestRunner
import sltest.plugins.ModelCoveragePlugin
import sltest.plugins.coverage.CoverageMetrics

2. Create the model coverage plugin object and the coverage metrics object. In this example, you use MCDC coverage and record coverage for referenced models.

mcdcMet = CoverageMetrics('Decision',true,'Condition',true,'MCDC',true);

covSettings = ModelCoveragePlugin('RecordModelReferenceCoverage',true,...
    'Collecting',mcdcMet);

3. Create a MATLAB® Unit Test test suite from the test file.

tf = sltest.testmanager.TestFile('AutopilotTestFile.mldatx');
APSuite = testsuite(tf.FilePath);

4. Create the test runner without any plugins, then add the coverage plugin to the runner.

APRun = TestRunner.withNoPlugins();
addPlugin(APRun,covSettings);

5. Run the suite.

% Turn off the command line warnings.
warning off Stateflow:cdr:VerifyDangerousComparison
warning off Stateflow:Runtime:TestVerificationFailed

APResult = run(APRun,APSuite)
Coverage Report for RollAutopilotMdlRef/Roll Reference
    /tmp/Bdoc22a_1891349_174088/tpe7f4dab9_9be8_4564_a914_a85e593aecf0.html
APResult = 
  TestResult with properties:

          Name: 'AutopilotTestFile > Basic Design Test Cases/Requirement 1.3 Test'
        Passed: 0
        Failed: 1
    Incomplete: 0
      Duration: 9.9864
       Details: [1x1 struct]

Totals:
   0 Passed, 1 Failed, 0 Incomplete.
   9.9864 seconds testing time.

6. You can open the link in the command-line output to view the coverage report.

Cleanup. Clear results and re-enable warnings.

warning on Stateflow:cdr:VerifyDangerousComparison
warning on Stateflow:Runtime:TestVerificationFailed

sltest.testmanager.clearResults;
sltest.testmanager.clear;
sltest.testmanager.close;

Version History

Introduced in R2018a