A MATLAB®-based Simulink® test is defined in a MATLAB code (
.m) file that you create in MATLAB, and then open,
run, and view results in the Test Manager. The test file is a class definition file that
The inheritance enables you to open the test file in the Test Manager. When you open a
MATLAB test file in the Test Manager, it appears and behaves the same as a test created in
the Test Manager, although with some limited functionality (see Limitations of MATLAB- based Tests). In addition to using
a MATLAB test in the Test Manager, you can use it at the command line like
any other unit test file.
Because these test files are text (
.m) files, you can edit, compare to
and merge with other
.m test files, and link from the file to
requirements. In contrast, test files created in the Test Manager or by using the
Test™ API are saved as binary
sltest.TestCase class and its methods work specifically with
MATLAB tests. You can use these methods in test files and at the command
line, except for
sltest.TestCase.forInteractiveUse, which can
only be used at the command line. In addition to these methods, you can use the
with MATLAB tests.
Class from which to inherit
Assume two sets of data are equivalent
Assert two sets of data are equivalent
Fatal assert two sets of data are equivalent
Verify two sets of data are equivalent
Create test case for use at command line
|Create temporary folder that is deleted when test case goes out of scope|
sltest.plugins classes enable functionality for MATLAB-based tests. In addition to these methods, you can use other
sltest.plugins classes with these tests. The plugins can be
attached to a
matlab.unittest.TestRunner to enable functionality
while running an
Enable integrating MATLAB test simulation and test results with the Test Manager
Enable writing text output to Test Manager results Logged Signals pane of the Test Manager
Enable collecting model coverage
To create a baseline MATLAB test:
Create a MATLAB code (
.m) file that defines the test cases.
You can launch the MATLAB Editor from the command line, or from the Test Manager by
using New > MATLAB-Based Simulink Test (.m).
See Author Class-Based Unit Tests in MATLAB. The only difference for MATLAB
tests is that the class must inherit from
instead of from
This sample MATLAB test file includes one test, which is defined in the
testOne function. When you run the test in the Test
Manager, the test loads the model named
sltestMATLABBasedTestExample. It then sets the value
gain2_var variable, and simulates the model.
Finally, the test compares model simulation output to the baseline data MAT
If a baseline data MAT-file does not already exist or if you need to update it, at the MATLAB command line, use:
For the sample file,
When you generate baselines, the test begins running. It pauses to open a
Simulation Data Inspector report, and you are prompted at the MATLAB command
line to review the baseline data. When you approve the data, it saves the
baseline data to a new MAT-file or updates the existing MAT-file. Then, the
test continues to run, but fails because the new or updated baseline data is
not included in the current run. Rerun the test using the
runtests command to use the new or updated baseline
and produce a passing result. You can also rerun the test by using the
rerun hyperlink at the command line.
Optionally, if you have a Simulink Coverage™ license, you can include coverage collection in your test. See Collect Coverage Using MATLAB-Based Simulink Tests for an example and information on coverage collection.
Optionally, if you have a Simulink Requirements™ license, you can add requirements. Open the Test Manager and update the test file.
Click Open > Open MATLAB-Based Simulink Test (.m) and select the test file. The test file loads and its test hierarchy displays in the Test Browser pane. If you select the test file, the Requirements and Test File Content panes appear in the Test Manager.
Add requirements by expanding the Requirements section by clicking Add to open the Outgoing Links Editor. See Link to Requirements for information on adding requirements.
To update the MATLAB code (
.m) test file from the Test Manager,
click the Open test in the MATLAB Editor link.
After you edit the
.m file and save your changes, or
after adding coverage or requirements, return to the Test Manager and click
the synchronization button next to the test file in the
Test Browser pane.
Run the test, view the results, and create a test results report.
Click Run to run the test.
To view the results, expand the rows in the Results and Artifacts pane.
To view coverage results, in the Results and Artifacts pane, select the Resultsitem and expand the Aggregated Coverage section. See Collect Coverage in Tests for information.
Optionally, create a test results report. See Generate Test Results Reports.
You must have Simulink Requirements to include requirements links.
To add links to requirements from a file being edited in the MATLAB Editor, see Requirements Traceability for MATLAB Code Lines (Simulink Requirements). For
MATLAB test files, you add links using the same process. However, the text you
select in the MATLAB code (
.m) file determines the type of link and the test
to which it is added. If you select:
Class definition line (e.g.,
classdef myClass <
sltest.TestCase) — Adds a
Verified By link
for the whole test file
Text inside a test function — Adds a
Verified By link
for that function
Text across multiple test functions — Adds a
By link for the first function in the selection
Any other text selected — Adds a
Related To link for the selection
After you add requirements links, you can view the verification status in the
Requirements Editor by clicking Display > Verification Status. To update the status of a
Verified By requirement,
right-click on the requirement and select Run Tests. See Summarize Requirements Verification Status (Simulink Requirements).
Links that you create in the MATLAB code (
.m) file appear in the
Requirements section of the Test Manager. Linking to
requirements from in the Test Manager works the same as described in Link to Requirements.
When you have the Requirements Editor open and you click on an incoming link that is for a MATLAB test, if you have a Simulink Test license, the Test Manager opens and goes to the associated test. If a license is not available, the MATLAB Editor opens and goes to the line of code associated with that requirement.
For a parameterized test, Simulink
Requirements does not support linking to individual parameterized versions of the test.
.m file, if you create a link from parameterized test to a
requirement, the link is associated with all
versions of that test. In the Test Manager, if you create a link from a version of a
parameterized test to a requirement, the link is associated with
all versions of that test.
MATLAB-based Simulink tests do not support:
Test types other than baseline tests.
Running tests in parallel.
Running tests in multiple releases.
Test tags and descriptions.
Callbacks. (However, while callbacks are not supported in the Test Manager for
MATLAB-based tests, you can use
TestMethodSetup, or fixtures in your
.m file to achieve similar functionality. See Write Setup and Teardown Code Using Classes.)
Logical and temporal assessments.