Main Content

Collect Coverage Using MATLAB-Based Simulink Tests

This example shows how to use a MATLAB®-based Simulink® test to collect coverage on a model with a test harness, and use the MATLAB Test Framework to populate the results in the Test Manager. MATLAB-based Simulink tests are MATLAB code (.m) file test case class definitions that inherit from sltest.TestCase.

MATLAB-Based Simulink Test File

The MATLAB-based Simulink test file, TestHarnessWithModelCoverage.m, has been created and is provided with this example. The test file contains two test functions. Each one has a harness model to drive input data to test the subsystem TestHarnessWithModelCoverage/Subsystem1 and compare with corresponding baseline. This test uses a Simulink.SimulationOutput object when simulating the model.

classdef TestHarnessWithModelCoverage < sltest.TestCase
   methods (Test)
      function testOne(testCase)
          in = testCase.createSimulationInput('simpleSwitchWithSubsystemIn',...
             'WithHarness','simpleSwitchWithSubsystemIn_Harness1');
          simOut = testCase.simulate(in);
          testCase.verifySignalsMatch(simOut,'baselineOne.mat');
      end
      function testTwo(testCase)
          in = testCase.createSimulationInput('simpleSwitchWithSubsystemIn',...
             'WithHarness','simpleSwitchWithSubsystemIn_Harness2');
          simOut = testCase.simulate(in);
          testCase.verifySignalsMatch(simOut,'baselineTwo.mat');
      end
   end
end

Create a TestRunner and Test Suite

Create a TestRunner to run the sltest_ratelim model.

import matlab.unittest.TestRunner;
runner = TestRunner.withTextOutput;

Create a TestSuite to use with the TestRunner.

suite = testsuite('TestHarnessWithModelCoverage');

Configure the Test Runner

Use plugin methods to configure the TestRunner to add test results from an sltest.TestCase to the Test Manager. Add the TestRunnerPlugin to the TestRunner.

import sltest.plugins.MATLABTestCaseIntegrationPlugin;
runner.addPlugin(MATLABTestCaseIntegrationPlugin);

The DiagnosticsOutputPlugin and the ToTestManagerLog stream the diagnostics from an sltest.TestCase run to the logs of TestCaseResults in the Test Manager. The diagnostics include passing diagnostics for tests that pass. Add the DiagnosticsOutputPlugin and ToTestManagerLog to the TestRunner.

import sltest.plugins.ToTestManagerLog;
import matlab.unittest.plugins.DiagnosticsOutputPlugin;
streamOutput = ToTestManagerLog();
diagnosticsOutputPlugin = DiagnosticsOutputPlugin...
    (streamOutput,'IncludingPassingDiagnostics',true);
runner.addPlugin(diagnosticsOutputPlugin);

Configure Coverage Collection for a Simulink Model

Models in an sltest.TestCase that are simulated using the simulate method can collect coverage. Use the ModelCoveragePlugin to configure coverage metrics collection. This example collects MCDC coverage. Add the ModelCoveragePlugin to the TestRunner.

import sltest.plugins.coverage.CoverageMetrics;
import sltest.plugins.ModelCoveragePlugin;
mcdcMetrics = CoverageMetrics('MCDC',true);
runner.addPlugin(ModelCoveragePlugin('Collecting',mcdcMetrics));

Alternatively, you can turn on coverage collection and set coverage metrics in the Test Manager. If you use this alternative, you do not need to import or add the coverage plugins to the runner.

Collect and Add Coverage and Test Results to the Test Manager

Now that the TestRunner is fully configured, use the run function to collect coverage and add the coverage and test results to the Test Manager.

run(runner,suite);
Setting up ResultSetFixture
Done setting up ResultSetFixture
__________

Running TestHarnessWithModelCoverage
..
Done TestHarnessWithModelCoverage
__________

Coverage Report for simpleSwitchWithSubsystemIn/Subsystem1
    /tmp/Bdoc24b_2679053_1870383/tp3e2326e2_0cc8_4894_9e4e_09339a6e5818.html
Tearing down ResultSetFixture
Done tearing down ResultSetFixture
__________

run also generates a report that includes cumulative coverage for the test suite that was run. Use the Coverage Report for sltest_ratelim link to view the report.

Open the Test Manager

sltest.testmanager.view

Select the Results and Artifacts pane and expand the Results and BaselineTestWithCoverage rows.

Select the testOne row.

The Coverage Results section shows the coverage collected for sltest_ratelim from testOne.

Select the testTwo row.

The Coverage Results section shows the coverage collected for sltest_ratelim from testTwo.

Select the BaselineTestWithCoverage row.

The Aggregated Coverage Results section shows the aggregation of the coverage collected for sltest_ratelim from testOne and testTwo. The aggregated results show full coverage for the specified coverage metrics.

See Also

| | | | | | | |