Main Content

sltest.plugins.ModelCoveragePlugin Class

Namespace: 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. To enable coverage collection, you must also add an instance of the sltest.plugins.MATLABTestCaseIntegrationPlugin to the test runner, or use the addSimulinkTestResults method, which is another way to add the sltest.plugins.MATLABTestCaseIntegrationPlugin.

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.

Note

If you open a MATLAB -based Simulink test file in the Test Manager, enable coverage, and set the coverage metrics, you do not need to use sltest.plugins.ModelCoveragePlugin. However, if you use sltest.plugins.ModelCoveragePlugin and also set coverage in the Test Manager, the Test Manager uses the settings in the sltest.plugins.ModelCoveragePlugin object.

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

Coverage collection for the referenced models, specified as a numeric or logical 1 (true) or 0 (false).

Example: 'RecordModelReferenceCoverage',true

Attributes:

SetAccess
public
GetAccess
public

Coverage collection options, specified as an sltest.plugins.coverage.CoverageMetrics object.

Example: 'Collecting',covSettings

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

Attributes:

SetAccess
public
GetAccess
public

Model coverage report options, specified as an sltest.plugins.coverage.ModelCoverageReport or a matlab.unittest.plugins.codecoverage.CoberturaFormat object. Model coverage reports in Cobertura format contain one line tag for each block in the Simulink model. The word line or lines in Cobertura report tags refers to a block. For example, lines-covered is the number of blocks executed in the test.

Example: 'Producing',mcr

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

Example: 'Producing',CoberturaFormat('reports/coverage/cobertura_modelcoverage')

Attributes:

SetAccess
public
GetAccess
public

Whether model implements, tests, and verifies requirements, specified as a numeric or logical 1 (true) or 0 (false).

Example: 'ScopeToRequirements',true

Attributes:

SetAccess
public
GetAccess
public

Examples

collapse all

This example shows how to use the MATLAB® unit testing framework 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
import sltest.plugins.coverage.ModelCoverageReport
import matlab.unittest.plugins.codecoverage.CoberturaFormat

2. Create a subfolder in the current working directory and create a ModelCoverageReport object specifying that new folder.

mkdir('./myReports/coverage');
path = './myReports/coverage';
mcr = ModelCoverageReport(path,'ReportName','RollRefCov');

3. 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,'Producing',mcr);

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

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

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

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

6. 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/Bdoc24b_2725827_3678106/tp9b7101d7/simulinktest-ex63642652/myReports/coverage/RollRefCov.html
APResult = 
  TestResult with properties:

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

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

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

8. You can also create a report in Cobertura XML format. Create a new test runner with no plugins, add the ModelCoveragePlugin, and run the test suite. The Cobertura report is saved to the working folder.

CobRun = TestRunner.withNoPlugins;
addPlugin(CobRun,ModelCoveragePlugin('Producing',...
    CoberturaFormat('CoverageResults.xml')));
CobResult = run(CobRun,APSuite);

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