Main Content

Using MATLAB-Based Simulink Tests in the Test Manager

This example shows how to create a MATLAB®-based Simulink® test, generate a baseline, and load, run, and view test results in the Test Manager. When you load a MATLAB-based Simulink test case code .m file into the Test Manager, the test case appears and behaves the same as a test case created directly in the Test Manager. The only difference is that for a MATLAB-based Simulink test, you can select or change coverage collection and metrics only at the test file level, not at either the test suite or test case level.

This example using internal test harness sltestMATLABBasedTestExample_harnrss verifies the sltestMATLABBasedTestExample model against a generated baseline.

Baseline Test Class Definition File

The class definition file, BaselineTest.m, has already been created and is provided with this example.

The test case file, BaselineTest.m, is derived from sltest.TestCase, which in turn is derived from matlab.unittest.TestCase. All of the matlab.unittest.TestCase methods are also available as a part of sltest.TestCase.

Baseline Test File Contents

The class definition file, BaselineTest.m, contains:

classdef BaselineTest < sltest.TestCase
    methods (Test)
       function testOne(testCase)
           testCase.loadSystem('sltestMATLABBasedTestExample');
           evalin('base','gain2_var = 2.01;');
           simOut = testCase.simulate('sltestMATLABBasedTestExample',...
             'WithHarness','sltestMATLAbBasedTestExample_harness');
           testCase.verifySignalsMatch(simOut,'baseline1.mat','AbsTol',0.015);
       end
    end
end

The file includes:

  • Inheritance from sltest.TestCase.

  • A test function named testOne, which is in a methods block that has the Test attribute.

The testOne function:

  • Uses the testCase.loadSystem method to load the sltestMATLABBasedTestExample model.

  • Changes the value of the gain2_var in the model to 2.01.

  • Uses the testCase.simulate method to simulate the model with the harness.

  • Uses the testCase.verifySignalsMatch method to compare the output of simulate, simOut, to the baseline data MAT-file named baseline1.mat. It also sets an absolute tolerance. If you remove the tolerance setting from the file before running the test, the test fails because the value of gain2_var was changed from its original value in the model.

Baseline Data File

The baseline data file, baseline1.mat, has already been generated and is provided with this example. The baseline data file was created using this process:

1. Use runtests('BaselineTest/testOne','GenerateBaselines',true).

2. After the baseline test runs, a Simulation Data Inspection report shows the output from the signals. View the Actual Results in the report and approve the baseline data. The data is saved in a MAT-file, which for this example is named baseline1.mat.

Open the MATLAB-based Simulink Test in the Test Manager

1. Open the Test Manager.

sltest.testmanager.view

2. In the Test Manager, click Open and select Open MATLAB-based Simulink Test (.m).

3. In the Open File dialog box, select BaselineTest.m.

The Test Manager populates the Test Browser with testOne from the BaselineTest.m file.

Set Up Coverage Collection

If you want to collect coverage for the test, select the BaselineTest file. In the Coverage Settings section, select the Coverage to Collect and the Coverage Metrics. If you want to collect coverage for the test, select the test file, BaselineTest, in the Test Browser pane. In the Coverage Settings section, select a Coverage to Collect option and Coverage Metrics options.

Run and Visualize the Results in the Test Manager

1. Click Run to execute the test.

2. After the test completes, expand all rows in the Results and Artifacts pane. Notice that testOne passes.

3. To view the data comparison, select Out2:1 under testOne > Simulation Output Comparison Results. The simulation and baseline signals match within the specified tolerance.

4. If you collected coverage, select the Baseline Test test file and view the Aggregated Coverage Results section.

4. If you collected coverage, select the BaselineTest test file and view the Aggregated Coverage Results section.

Clear and close the Test Manager

sltest.testmanager.clear
sltest.testmanager.clearResults
sltest.testmanager.close

Related Topics