www.gusucode.com > simulinktest 案例源码程序 matlab代码 > simulinktest/ProgrammaticallyCreateTestSequenceExample.m

    %% Programmatically Create a Test Sequence
% This example shows how to create a test sequence programmatically. You
% create a Test Sequence block, and author a test sequence to verify two
% safety requirements of a cruise control system.

%% Create a Test Harness Containing a Test Sequence Block
%
% 1. Open the cruise control project, and open the model. This creates a
% working copy of the project in your MATLAB folder.
slVerificationCruiseStart;
open_system simulinkCruiseAddReqExample.slx

%%
% 2. Create and open the test harness.
sltest.harness.create('simulinkCruiseAddReqExample','Name','SafetyTestHarness',...
    'Source','Test Sequence')
sltest.harness.open('simulinkCruiseAddReqExample','SafetyTestHarness')
set_param('SafetyTestHarness','StopTime','15');

%% Author the Test Sequence
%
% 1. Add a local variable |endTest|, which you use to transition between
% test steps.
a = sltest.testsequence.addSymbol('SafetyTestHarness/Test Sequence','endTest',...
    'Data','Local');
a.DataType = 'boolean';

%%
% 2. Change the name of the step |Run| to |Initialize1|.
sltest.testsequence.editStep('SafetyTestHarness/Test Sequence','Run',...
    'Name','Initialize1');

%%
% 3. Add a step |BrakeTest| to test that the cruise control disengages when
% the brake is applied. Also add substeps defining the test scenario
% actions and verification.
sltest.testsequence.addStepAfter('SafetyTestHarness/Test Sequence',...
    'BrakeTest','Initialize1','Label','endTest = false;')
    
    % Add a transition from |Initialize1| to |BrakeTest|.
    sltest.testsequence.addTransition('SafetyTestHarness/Test Sequence',...
        'Initialize1','true','BrakeTest')

    % This sub-step enables the cruise control and sets the speed.
    % |SetValuesActions| is the actions for BrakeTest.SetValues.
    setValuesActions = sprintf('CruiseOnOff = true;\nSpeed = single(50);');
    sltest.testsequence.addStep('SafetyTestHarness/Test Sequence',...
        'BrakeTest.SetValues','Label',setValuesActions)
    
    % This sub-step engages the cruise control.
    setCCActions = sprintf('CoastSetSw = true;');
    sltest.testsequence.addStepAfter('SafetyTestHarness/Test Sequence',...
        'BrakeTest.Engage','BrakeTest.SetValues','Label',setCCActions)
    
    % This step applies the brake.
    brakeActions = sprintf('CoastSetSw = false;\nBrake = true;');
    sltest.testsequence.addStepAfter('SafetyTestHarness/Test Sequence',...
        'BrakeTest.Brake','BrakeTest.Engage','Label',brakeActions)
    
    % This step verifies that the cruise control is off.
    brakeVerifyActions = sprintf('verify(engaged == false)\nendTest = true;');
    sltest.testsequence.addStepAfter('SafetyTestHarness/Test Sequence',...
        'BrakeTest.Verify','BrakeTest.Brake','Label',brakeVerifyActions)
    
    % Add transitions between steps.
    sltest.testsequence.addTransition('SafetyTestHarness/Test Sequence',...
        'BrakeTest.SetValues','true','BrakeTest.Engage')
    sltest.testsequence.addTransition('SafetyTestHarness/Test Sequence',...
        'BrakeTest.Engage','after(2,sec)','BrakeTest.Brake')
    sltest.testsequence.addTransition('SafetyTestHarness/Test Sequence',...
        'BrakeTest.Brake','true','BrakeTest.Verify')

%%
% 4. Add a step |Initialize2| to initialize component inputs again, and add
% a transition from |BrakeTest| to |Initialize2|.
init2Actions = sprintf(['CruiseOnOff = false;\n'...
    'Brake = false;\n'...
    'Speed = single(0);\n'...
    'CoastSetSw = false;\n'...
    'AccelResSw = false;']);
sltest.testsequence.addStepAfter('SafetyTestHarness/Test Sequence',...
    'Initialize2','BrakeTest','Label',init2Actions)
sltest.testsequence.addTransition('SafetyTestHarness/Test Sequence',...
    'BrakeTest','endTest == true','Initialize2')

%%
% 5. Add a step |LimitTest| to test cruise control disengagement when the
% vehicle speed exceeds the high limit. Also, add a transition from the
% |Initialize2| step, and add sub-steps to define the scenario actions and
% verification.
sltest.testsequence.addStepAfter('SafetyTestHarness/Test Sequence',...
    'LimitTest','Initialize2')
sltest.testsequence.addTransition('SafetyTestHarness/Test Sequence',...
    'Initialize2','true','LimitTest')

    % Add a step to enable cruise control and set the speed.
    setValuesActions2 = sprintf('CruiseOnOff = true;\nSpeed = 60;');
    sltest.testsequence.addStep('SafetyTestHarness/Test Sequence',...
        'LimitTest.SetValues','Label',setValuesActions2)
    
    % Add a step to engage the cruise control.
    setCCActions = sprintf('CoastSetSw = true;');
    sltest.testsequence.addStepAfter('SafetyTestHarness/Test Sequence',...
        'LimitTest.Engage','LimitTest.SetValues','Label',setCCActions)

    % Add a step to ramp the vehicle speed.
    sltest.testsequence.addStepAfter('SafetyTestHarness/Test Sequence',...
        'LimitTest.RampUp','LimitTest.Engage','Label','Speed = Speed + ramp(5*et);')
    
    % Add a step to verify that the cruise control is off.
    highLimVerifyActions = sprintf('verify(engaged == false)');
    sltest.testsequence.addStepAfter('SafetyTestHarness/Test Sequence',...
        'LimitTest.VerifyHigh','LimitTest.RampUp','Label',highLimVerifyActions)
    
    % Add transitions between steps. The speed ramp transitions when the
    % vehicle speed exceeds 90.
    sltest.testsequence.addTransition('SafetyTestHarness/Test Sequence',...
        'LimitTest.SetValues','true','LimitTest.Engage')
    sltest.testsequence.addTransition('SafetyTestHarness/Test Sequence',...
        'LimitTest.Engage','true','LimitTest.RampUp')
    sltest.testsequence.addTransition('SafetyTestHarness/Test Sequence',...
        'LimitTest.RampUp','Speed > 90','LimitTest.VerifyHigh')

%%
% Double-click the Test Sequence block to open the editor and view the
% created test sequence.
%
% <<../test_sequence.png>>
    
%% Close the Test Harness and Model
sltest.harness.close('simulinkCruiseAddReqExample','SafetyTestHarness');
close_system('simulinkCruiseAddReqExample.slx',0);