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

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 BaselineTestWithCoverage
..
Done BaselineTestWithCoverage
__________

Tearing down ResultSetFixture
Done tearing down ResultSetFixture
__________

Coverage Report for sltest_ratelim
    /tmp/BR2020bd_1444674_32127/publish_examples0/tpa9c5d37f_9574_4bc3_9bfc_0fa1f056c7d7.html

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

sltestmgr

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.