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 that contains a Test Sequence block. You use the block to author a series of test steps that verify two functional attributes of a cruise control system.
Load the Model
model = 'sltestCruiseChart';
load_system(model)
Create a Test Harness Containing a Test Sequence Block
Create and load the test harness. Set the harness stop time to 15 seconds.
sltest.harness.create(model,'Name','Harness1',... 'Source','Test Sequence'); sltest.harness.load(model,'Harness1'); set_param('Harness1','StopTime','15');
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. Variables in test sequences are called symbols.
sltest.testsequence.addSymbol('Harness1/Test Sequence','endTest',... 'Data','Local'); sltest.testsequence.editSymbol('Harness1/Test Sequence','endTest',... 'DataType','boolean');
2. Change the name of the Run
step to Initialize1
.
sltest.testsequence.editStep('Harness1/Test Sequence','Run',... 'Name','Initialize1');
3. Add a BrakeTest
step, which checks that the cruise control disengages when the brake is applied. Add substeps to the BrakeTest
step that define the test scenario actions and verifications.
sltest.testsequence.addStepAfter('Harness1/Test Sequence',... 'BrakeTest','Initialize1','Action','endTest = false;') % Transition from |Initialize1| to |BrakeTest|. sltest.testsequence.addTransition('Harness1/Test Sequence',... 'Initialize1','true','BrakeTest') % Enable the cruise control and set 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) % Engage the cruise control. setCCActions = sprintf('CoastSetSw = true;'); sltest.testsequence.addStepAfter('Harness1/Test Sequence',... 'BrakeTest.Engage','BrakeTest.SetValues','Action',setCCActions) % Apply the brake. brakeActions = sprintf('CoastSetSw = false;\nBrake = true;'); sltest.testsequence.addStepAfter('Harness1/Test Sequence',... 'BrakeTest.Brake','BrakeTest.Engage','Action',brakeActions) % Verify 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 second step Initialize2
to initialize component inputs and 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 LimitTest
step, which checks that the cruise control disengages when the vehicle speed exceeds the high limit. Add a transition from the Initialize2
step, and add substeps to define the actions and verification.
sltest.testsequence.addStepAfter('Harness1/Test Sequence',... 'LimitTest','Initialize2') sltest.testsequence.addTransition('Harness1/Test Sequence',... 'Initialize2','true','LimitTest') % Enable cruise control and set the speed. setValuesActions2 = sprintf('CruiseOnOff = true;\nSpeed = 60;'); sltest.testsequence.addStep('Harness1/Test Sequence',... 'LimitTest.SetValues','Action',setValuesActions2) % Engage the cruise control. setCCActions = sprintf('CoastSetSw = true;'); sltest.testsequence.addStepAfter('Harness1/Test Sequence',... 'LimitTest.Engage','LimitTest.SetValues','Action',setCCActions) % Ramp up the vehicle speed. sltest.testsequence.addStepAfter('Harness1/Test Sequence',... 'LimitTest.RampUp','LimitTest.Engage','Action', ... 'Speed = Speed + ramp(5*et);') % 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.
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
sltest.harness.create
| sltest.testsequence.addSymbol
| sltest.testsequence.editSymbol
| sltest.testsequence.addStep
| sltest.testsequence.addStepAfter
| sltest.testsequence.addTransition