sltest.plugins.ModelCoveragePlugin class

Package: sltest.plugins

Collect model coverage using the MATLAB Unit Test framework

Description

The sltest.plugins.ModelCoveragePlugin class creates a coverage collection object for running Simulink® Test™ test cases with the MATLAB® Unit Test framework. Add the sltest.plugins.ModelCoveragePlugin object to the test runner.

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 the object:

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 a sltest.plugins.coverage.ModelCoverageReport object.

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(fullfile(matlabroot,'toolbox','simulinktest',...
    'simulinktestdemos','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 > Basic Design Test Cases/Requirement 1.3 Test'
        Passed: 0
        Failed: 1
    Incomplete: 1
      Duration: 0.0269
       Details: [1x1 struct]

Totals:
   0 Passed, 1 Failed (rerun), 1 Incomplete.
   0.026932 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