Test Models Using MATLAB Unit Test

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

  • Allows you to execute model tests together with MATLAB 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 MATLAB Unit Test:

  1. Create a TestSuite from the Simulink Test file.

  2. Create a TestRunner.

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

  4. Add the plugins to the TestRunner.

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

Considerations

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

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

Comparison of Test Nomenclature

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

  • If the test case contains iterations, the MATLAB Unit Test contains parameterizations.

  • If the test file or test suite contains callbacks, the MATLAB Unit Test contains one or more callbacks fixtures.

Test Case Iterations and MATLAB 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 MATLAB 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

FileCallbacksFixture

Test suite callbacks

SuiteCallbacksFixture

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. Run the test, creating a TestResult object. The command window returns warnings from the verify statement failures.

apresults = run(apsuite);
Running AutopilotTestFile > Basic Design Test Cases

================================================================================
Verification failed in AutopilotTestFile > Basic Design Test Cases/Requirement 1.3 Test.
    ---------------------
    Framework Diagnostic:
    ---------------------
    Failed criteria: Verification
    --> Simulink Test Manager Results:
            Results: 2020-Aug-20 14:25:16/AutopilotTestFile/Basic Design Test Cases/Requirement 1.3 Test
================================================================================
.
Done AutopilotTestFile > Basic Design Test Cases
__________

Failure Summary:

     Name                                                              Failed  Incomplete  Reason(s)
    ===============================================================================================================
     AutopilotTestFile > Basic Design Test Cases/Requirement 1.3 Test    X                 Failed by verification.

4. To view the details of the test, display the Report property of the DiagnosticRecord object. The record shows that a verification failed during the test.

apresults.Details.DiagnosticRecord.Report
ans = 
    'Verification failed in AutopilotTestFile > Basic Design Test Cases/Requirement 1.3 Test.
         ---------------------
         Framework Diagnostic:
         ---------------------
         Failed criteria: Verification
         --> Simulink Test Manager Results:
                 Results: 2020-Aug-20 14:25:16/AutopilotTestFile/Basic Design Test Cases/Requirement 1.3 Test'

Enable warnings.

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

See Also

| | | |

Related Topics