主要内容

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。mfile into the Test Manager, the test case appears and behaves the same as any test case created directly in the Test Manager.

This example using internal test harnesssltestMATLABBasedTestExample_harnrssverifies thesltestMATLABBasedTestExamplemodel against a generated baseline.

基线测试类定义文件

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

The test case file,BaselineTest.m, is derived fromsltest.TestCase, which in turn is derived frommatlab.unittest.TestCase。All of thematlab.unittest.TestCasemethods are also available as a part ofsltest.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 fromsltest.TestCase

  • A test function namedtestOne, which is in amethodsblock that has theTestattribute.

ThetestOnefunction:

  • Uses thetestCase.loadSystem方法来加载sltestMATLABBasedTestExamplemodel.

  • Changes the value of thegain2_varin the model to 2.01.

  • Uses thetestCase.simulatemethod to simulate the model with the harness.

  • Uses thetestCase.verifySignalsMatchmethod to compare the output ofsimulate,simOut, to the baseline data MAT-file namedbaseline1.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 ofgain2_varwas 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. Useruntests('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 namedbaseline1.mat

Open the MATLAB-based Simulink Test in the Test Manager

1. Open the Test Manager.

sltestmgr

2. In the Test Manager, clickOpenand selectOpen MATLAB-based Simulink Test (.m)

3. In the Open File dialog box, selectBaselineTest.m

The Test Manager populates theTest BrowserwithtestOnefrom theBaselineTest.mfile.

Run and Visualize the Results in the Test Manager

1. ClickRunto execute the test.

2. After the test completes, expand all rows in theResults and Artifactspane. Notice thattestOnepasses.

3. To view the data comparison, selectOut2:1undertestOne>Simulation Output Comparison Results。的仿真on and baseline signals match within the specified tolerance.

Related Topics