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.

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.

Example: 'Collecting',covSettings

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

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',false,'Condition',false,'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)
APResult = 
  TestResult with properties:

          Name: 'AutopilotTestFile > New Test Suite 1/New Test Case 1'
        Passed: 0
        Failed: 1
    Incomplete: 0
      Duration: 0.2135
       Details: [1x1 struct]

Totals:
   0 Passed, 1 Failed (rerun), 0 Incomplete.
   0.21352 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;
Introduced in R2018a