Main Content

Test Models Using MATLAB Unit Test

You can use the MATLAB® Unit Test framework to run tests authored in Simulink® Test™. Using the framework:

  • Allows you to execute model tests together with Unit Test scripts, functions, and classes.

  • Enables model and code testing using the same framework.

  • Enables integration with continuous integration (CI) systems, such as Jenkins®.

Overall Workflow

To run tests with the Unit Test framework:

  1. Create a TestSuite from the Simulink Test file.

  2. Create a TestRunner.

  3. Create plugin objects to customize the TestRunner. For example:

    • The matlab.unittest.plugins.TAPPlugin produces a results stream according to the Test Anything Protocol for use with certain CI systems.

    • The sltest.plugins.ModelCoveragePlugin specifies model coverage collection and lets you return coverage results at to the command line. If you set up coverage in the Test Manager, you do not need to use this plugin.

  4. Add the plugins to the TestRunner.

  5. Run the test using the run method, or run tests in parallel using the runInParallel method.


When running tests using the Unit Test framework, consider the following:

  • If you disable a test in the Test Manager, the test is filtered using Unit Test, and the result reflects a failed assumption.

Comparison of Test Nomenclature

The Unit Test framework has analogous properties to the functionality in Simulink Test. For example,

  • If the test case contains iterations, the unit test contains parameterizations.

  • If the test file or test suite contains callbacks, the unit test contains one or more callbacks fixtures.

Test Case Iterations and Unit Test Parameterizations

Parameterization details correspond to properties of the iteration.

Simulink Test

MATLAB Unit Test

Iteration type: Scripted

Parameterization property: ScriptedIteration

Iteration type: Table

Parameterization property: TableIteration

Iteration name

Parameterization Name

Test case iteration object

Parameterization Value

Test Callbacks and Unit Test Fixtures

Fixtures depend on callbacks contained in the test file. Fixtures do not include test case callbacks, which are executed with the test case itself.

Callbacks in Simulink Test

Fixtures in MATLAB Unit Test

Test file callbacks


Test suite callbacks


File and suite callbacks

Heterogeneous CallbacksFixture, containing FileCallbacksFixture and SuiteCallbacksFixture

No callbacks

No fixture

Basic Workflow Using MATLAB® Unit Test

This example shows how to create and run a basic MATLAB® Unit Test for a test file created in Simulink® Test™. You create a test suite, run the test, and display the diagnostic report.

Before running this example, temporarily disable warnings that result from verification failures.

warning off Stateflow:Runtime:TestVerificationFailed;
warning off Stateflow:cdr:VerifyDangerousComparison;

1. Author a test file in the Test Manager, or start with a preexisting test file. For this example, AutopilotTestFile tests a component of an autopilot system against several requirements, using verify statements.

2. Create a TestSuite from the test file.

apsuite = testsuite('AutopilotTestFile.mldatx');

3. Create the test runner.

import matlab.unittest.TestRunner
aprunner = TestRunner.withNoPlugins;

4. Add the plugin to produce Test Manager results.

import sltest.plugins.TestManagerResultsPlugin
tmr = TestManagerResultsPlugin; 

5. Run the test.

apresults = run(aprunner,apsuite);

6. View the summary of the test, which shows that the test failed because of a verification failure.

ans = 
  TestResultContainer with properties:

    TestResult: [1x1 sltest.testmanager.TestCaseResult]

Enable warnings.

warning on Stateflow:Runtime:TestVerificationFailed;
warning on Stateflow:cdr:VerifyDangerousComparison;

See Also

| | | |

Related Topics