Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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™.

To use MATLAB Unit Test, create a TestSuite from the Simulink Test file. To customize the test execution, such as for CI, create a TestRunner. Running the test produces a TestResult object. For CI, running the test can also stream results to a file.

Considerations

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

  • Test hierarchy from Simulink Test is not converted by MATLAB Unit Test. All tests in a TestSuite are contained in a flat hierarchy.

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

  • Fast restart is not supported for the MATLAB Unit Test framework.

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
================================================================================
.
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
     ================================================================================'

Enable warnings.

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

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

Test a Model for Continuous Integration Systems

This example shows how to use MATLAB® Unit Test to test a model, and use the TAPPlugin to create TAP results. You can use TAP with CI systems. The model is a controller-plant system of a flight controller, aircraft model, and environment model.

Create a test suite and a test runner, and customize the runner with the plugin that creates the TAP file. When you run the test, it fails on several iterations. The results are written to the TAP file.

Before performing this example, set the working directory to a writable location on the path.

1. Open the Model

open_system(fullfile(matlabroot,'examples','simulinktest',...
    'sltestF14ParameterSweep.slx'))

2. Open the Test File

The test case creates a square wave input to the controller, and sweeps through 25 iterations of the parameters a and b. It compares the alpha output to a baseline with a tolerance of 0.0046 and fails any output which exceeds this tolerance.

sltest.testmanager.view;
sltest.testmanager.load(fullfile(matlabroot,'examples','simulinktest',...
    'f14ParameterSweepTest.mldatx'));

3. Import the TestRunner, TestSuite, TAPPlugin, and ToFile classes.

import matlab.unittest.TestRunner
import matlab.unittest.TestSuite
import matlab.unittest.plugins.TAPPlugin
import matlab.unittest.plugins.ToFile

4. Create the test suite object.

suite = testsuite(fullfile(matlabroot,'examples','simulinktest',...
    'f14ParameterSweepTest.mldatx'))
suite = 

  1×25 Test array with properties:

    Name
    BaseFolder
    ProcedureName
    SharedTestFixtures
    Parameterization
    Tags

Tests Include:
   25 Unique Parameterizations, 0 Shared Test Fixture Classes, 0 Tags.

5. Create the test runner object, and set it to display output to the command window.

f14runner = TestRunner.withTextOutput;

6. Create a TAP plugin that sends output to the file F14TapOutput.tap.

tapFile = 'F14TapOutput.tap';
plugin = TAPPlugin.producingVersion13(ToFile(tapFile));

7. Add the plugin to the test runner.

addPlugin(f14runner,plugin)

8. Run the test. The test fails several iterations in which the delta between the signal output and the baseline exceeds the tolerance.

result = run(f14runner,suite);
Running f14ParameterSweepTest > New Test Suite 1
..........
........
================================================================================
Verification failed in f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration19).

    ---------------------
    Framework Diagnostic:
    ---------------------
    Failed criteria: Baseline
    --> Logs:
            Inputs may not be compatible for simulation. Test results might not be accurate. Click here for more information on external input mapping.
================================================================================
..
...
================================================================================
Verification failed in f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration24).

    ---------------------
    Framework Diagnostic:
    ---------------------
    Failed criteria: Baseline
    --> Logs:
            Inputs may not be compatible for simulation. Test results might not be accurate. Click here for more information on external input mapping.
================================================================================
.
================================================================================
Verification failed in f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration25).

    ---------------------
    Framework Diagnostic:
    ---------------------
    Failed criteria: Baseline
    --> Logs:
            Inputs may not be compatible for simulation. Test results might not be accurate. Click here for more information on external input mapping.
================================================================================
.
Done f14ParameterSweepTest > New Test Suite 1
__________

Failure Summary:

     Name                                                                                                         Failed  Incomplete  Reason(s)
    ==========================================================================================================================================================
     f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration19)    X                 Failed by verification.
    ----------------------------------------------------------------------------------------------------------------------------------------------------------
     f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration24)    X                 Failed by verification.
    ----------------------------------------------------------------------------------------------------------------------------------------------------------
     f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration25)    X                 Failed by verification.
    

9. Display the results from the TAP file.

disp(fileread(tapFile))
TAP version 13
1..25
ok 1 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration1)
ok 2 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration2)
ok 3 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration3)
ok 4 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration4)
ok 5 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration5)
ok 6 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration6)
ok 7 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration7)
ok 8 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration8)
ok 9 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration9)
ok 10 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration10)
ok 11 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration11)
ok 12 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration12)
ok 13 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration13)
ok 14 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration14)
ok 15 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration15)
ok 16 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration16)
ok 17 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration17)
ok 18 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration18)
not ok 19 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration19)
    ---
    Event:
        Event Name: 'VerificationFailed'
        Event Location: 'f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration19)'
        Framework Diagnostic: |
            Failed criteria: Baseline
            --> Logs:
                    Inputs may not be compatible for simulation. Test results might not be accurate. Click here for more information on external input mapping.
    ...
ok 20 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration20)
ok 21 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration21)
ok 22 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration22)
ok 23 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration23)
not ok 24 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration24)
    ---
    Event:
        Event Name: 'VerificationFailed'
        Event Location: 'f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration24)'
        Framework Diagnostic: |
            Failed criteria: Baseline
            --> Logs:
                    Inputs may not be compatible for simulation. Test results might not be accurate. Click here for more information on external input mapping.
    ...
not ok 25 - f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration25)
    ---
    Event:
        Event Name: 'VerificationFailed'
        Event Location: 'f14ParameterSweepTest > New Test Suite 1/Iterations Parameter Sweep(ScriptedIteration=Scripted_Iteration25)'
        Framework Diagnostic: |
            Failed criteria: Baseline
            --> Logs:
                    Inputs may not be compatible for simulation. Test results might not be accurate. Click here for more information on external input mapping.
    ...


sltest.testmanager.clearResults
sltest.testmanager.clear
sltest.testmanager.close
close_system('sltestF14ParameterSweep',0)

See Also

| | | |

Related Topics

Was this topic helpful?