Main Content

sltest.plugins.coverage.CoverageMetrics Class

Namespace: sltest.plugins.coverage

Specify coverage metrics for tests run with MATLAB Unit Test framework

Description

Use the sltest.plugins.coverage.CoverageMetrics class to specify coverage metrics. Pass the coverage metrics object to the model coverage plugin object.

The sltest.plugins.coverage.CoverageMetrics class is a handle class.

Class Attributes

HandleCompatible
true

For information on class attributes, see Class Attributes.

Creation

cmo = sltest.plugins.coverage.CoverageMetrics(Properties) creates a coverage metrics object with specified properties.

You can also import the plugin, then use the class name to create the object:

import sltest.plugins.coverage.CoverageMetrics
cmo = CoverageMetrics(Properties)

Properties

expand all

Enable or disable decision coverage collection.

Example: 'Decision',true

Attributes:

SetAccess
public
GetAccess
public

Enable or disable condition coverage collection. If you enable Condition coverage, the Coverage Report shows both Condition and Decision coverage.

Example: 'Condition',true

Attributes:

SetAccess
public
GetAccess
public

Enable or disable modified condition/decision coverage collection. If you enable MCDC, Condition and Decision coverage are also collected and are included in the Coverage Report.

Example: 'MCDC',true

Attributes:

SetAccess
public
GetAccess
public

Enable or disable lookup table coverage collection.

Example: 'LookupTable',true

Attributes:

SetAccess
public
GetAccess
public

Enable or disable signal range coverage collection.

Example: 'SignalRange',true

Attributes:

SetAccess
public
GetAccess
public

Enable or disable signal size coverage collection.

Example: 'SignalSize',true

Attributes:

SetAccess
public
GetAccess
public

Enable or disable Simulink Design Verifier block coverage collection.

Example: 'SimulinkDesignVerifier',true

Attributes:

SetAccess
public
GetAccess
public

Enable or disable recording the number of times the block saturates on integer overflow.

Example: 'SaturationOnIntegerOverflow',true

Attributes:

SetAccess
public
GetAccess
public

Enable or disable relational boundary coverage.

Example: 'RelationalBoundary',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_2679053_1875008/tp29e4a166/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.2058
       Details: [1x1 struct]

Totals:
   1 Passed, 0 Failed, 0 Incomplete.
   10.2058 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