MATLAB Examples

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);
Setting up FileCallbacksFixture
Done setting up FileCallbacksFixture: Invoked setup callback of "AutopilotTestFile".
__________

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: 2017-Jul-31 21:10:11/Requirement 1.3 Test
================================================================================
.
Done AutopilotTestFile > Basic Design Test Cases
__________

Tearing down FileCallbacksFixture
Done tearing down FileCallbacksFixture: Invoked cleanup callback of "AutopilotTestFile".
__________

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: 2017-Jul-31 21:10:11/Requirement 1.3 Test
     ================================================================================'

Enable warnings.

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