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, and use the MATLAB Test Framework to populate the results in the Test Manager. MATLAB-based Simulink tests are .m file test case class definitions that inherit from sltest.TestCase.

MATLAB-Based Simulink Test File

The MATLAB-based Simulink test file, BaselineTestWithCoverage.m, has been created and is provided with this example. The test file contains two test functions. The testOne function is a simple baseline test for a rate limiting model, sltest_ratelim.

The second test function, testTwo, changes some variables in the model workspace. The changed variables simulate the model with a falling gain that exceeds the rate limit. Then, the rate limit changes so that the gain values are within the limit. This test uses a Simulink.SimulationOutput object when simulating the model.

Create a TestRunner and TestSuite

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('BaselineTestWithCoverage');

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;

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...

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);

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.

Setting up ResultSetFixture
Done setting up ResultSetFixture

Running BaselineTestWithCoverage
Done BaselineTestWithCoverage

Coverage Report for sltest_ratelim
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


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.