Main Content

Programmatically Create a Test Sequence

This example shows how to create a test harness and test sequence using the programmatic interface. You create a test harness and a Test Sequence block, and author a test sequence to verify two functional attributes of a cruise control system.

Create a Test Harness Containing a Test Sequence Block

1. Load the model.

model = 'sltestCruiseChart';
load_system(model)

2. Create the test harness.

sltest.harness.create(model,'Name','Harness1',...
    'Source','Test Sequence')
sltest.harness.load(model,'Harness1');
set_param('Harness1','StopTime','15');
ans = 

  struct with fields:

                    model: 'sltestCruiseChart'
                     name: 'Harness1'
              description: ''
              ownerHandle: 152.0010
            ownerFullPath: 'sltestCruiseChart'
                ownerType: 'Simulink.BlockDiagram'
         verificationMode: 'Normal'
           saveExternally: 0
            rebuildOnOpen: 0
         rebuildModelData: 0
      postRebuildCallback: ''
                graphical: 0
                  origSrc: 'Test Sequence'
                 origSink: 'Test Assessment'
      synchronizationMode: 'SyncOnOpen'
      existingBuildFolder: ''
    functionInterfaceName: ''

Author the Test Sequence

1. Add a local variable endTest and set the data type to boolean. You use endTest to transition between test steps.

sltest.testsequence.addSymbol('Harness1/Test Sequence','endTest',...
    'Data','Local');

sltest.testsequence.editSymbol('Harness1/Test Sequence','endTest',...
    'DataType','boolean');

2. Change the name of the step Run to Initialize1.

sltest.testsequence.editStep('Harness1/Test Sequence','Run',...
    'Name','Initialize1');

3. Add a step BrakeTest. BrakeTest checks that the cruise control disengages when the brake is applied. Add substeps defining the test scenario actions and verification.

sltest.testsequence.addStepAfter('Harness1/Test Sequence',...
    'BrakeTest','Initialize1','Action','endTest = false;')

    % Add a transition from |Initialize1| to |BrakeTest|.
    sltest.testsequence.addTransition('Harness1/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 = 50;');
    sltest.testsequence.addStep('Harness1/Test Sequence',...
        'BrakeTest.SetValues','Action',setValuesActions)

    % This sub-step engages the cruise control.
    setCCActions = sprintf('CoastSetSw = true;');
    sltest.testsequence.addStepAfter('Harness1/Test Sequence',...
        'BrakeTest.Engage','BrakeTest.SetValues','Action',setCCActions)

    % This step applies the brake.
    brakeActions = sprintf('CoastSetSw = false;\nBrake = true;');
    sltest.testsequence.addStepAfter('Harness1/Test Sequence',...
        'BrakeTest.Brake','BrakeTest.Engage','Action',brakeActions)

    % This step verifies that the cruise control is off.
    brakeVerifyActions = sprintf('verify(engaged == false)\nendTest = true;');
    sltest.testsequence.addStepAfter('Harness1/Test Sequence',...
        'BrakeTest.Verify','BrakeTest.Brake','Action',brakeVerifyActions)

    % Add transitions between steps.
    sltest.testsequence.addTransition('Harness1/Test Sequence',...
        'BrakeTest.SetValues','true','BrakeTest.Engage')
    sltest.testsequence.addTransition('Harness1/Test Sequence',...
        'BrakeTest.Engage','after(2,sec)','BrakeTest.Brake')
    sltest.testsequence.addTransition('Harness1/Test Sequence',...
        'BrakeTest.Brake','true','BrakeTest.Verify')

4. Add a step Initialize2 to initialize component inputs. Add a transition from BrakeTest to Initialize2.

init2Actions = sprintf(['CruiseOnOff = false;\n'...
    'Brake = false;\n'...
    'Speed = 0;\n'...
    'CoastSetSw = false;\n'...
    'AccelResSw = false;']);
sltest.testsequence.addStepAfter('Harness1/Test Sequence',...
    'Initialize2','BrakeTest','Action',init2Actions)
sltest.testsequence.addTransition('Harness1/Test Sequence',...
    'BrakeTest','endTest == true','Initialize2')

5. Add a step LimitTest. LimitTest checks that the cruise control disengages when the vehicle speed exceeds the high limit. Add a transition from the Initialize2 step, and add sub-steps to define the actions and verification.

sltest.testsequence.addStepAfter('Harness1/Test Sequence',...
    'LimitTest','Initialize2')
sltest.testsequence.addTransition('Harness1/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('Harness1/Test Sequence',...
        'LimitTest.SetValues','Action',setValuesActions2)

    % Add a step to engage the cruise control.
    setCCActions = sprintf('CoastSetSw = true;');
    sltest.testsequence.addStepAfter('Harness1/Test Sequence',...
        'LimitTest.Engage','LimitTest.SetValues','Action',setCCActions)

    % Add a step to ramp the vehicle speed.
    sltest.testsequence.addStepAfter('Harness1/Test Sequence',...
        'LimitTest.RampUp','LimitTest.Engage','Action','Speed = Speed + ramp(5*et);')

    % Add a step to verify that the cruise control is off.
    highLimVerifyActions = sprintf('verify(engaged == false)');
    sltest.testsequence.addStepAfter('Harness1/Test Sequence',...
        'LimitTest.VerifyHigh','LimitTest.RampUp','Action',highLimVerifyActions)

    % Add transitions between steps. The speed ramp transitions when the
    % vehicle speed exceeds 90.
    sltest.testsequence.addTransition('Harness1/Test Sequence',...
        'LimitTest.SetValues','true','LimitTest.Engage')
    sltest.testsequence.addTransition('Harness1/Test Sequence',...
        'LimitTest.Engage','true','LimitTest.RampUp')
    sltest.testsequence.addTransition('Harness1/Test Sequence',...
        'LimitTest.RampUp','Speed > 90','LimitTest.VerifyHigh')

Open the test harness to view the test sequence.

sltest.harness.open(model,'Harness1');

Double-click the Test Sequence block to open the editor and view the test sequence.

Close the Test Harness and Model

sltest.harness.close(model,'Harness1');
close_system(model,0);

See Also