Main Content

本页采用了机器翻译。点击此处可查看英文原文。

以编程方式创建测试序列

此示例显示如何使用编程接口创建测试框架和测试序列。您创建一个测试框架和一个 Test Sequence 模块,并编写一个测试序列来验证巡航控制系统的两个功能属性。

创建包含 Test Sequence 模块的测试框架

1.加载模型。

model = 'sltestCruiseChart';
load_system(model)

2.创建测试框架。

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.0006
            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: ''

编写测试序列

1.添加局部变量 endTest 并将数据类型设置为 boolean。您使用 endTest 在测试步骤之间转移。

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

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

2.将步骤的名称从 Run 更改为 Initialize1

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

3.添加步骤 BrakeTestBrakeTest 检查踩下刹车时巡航控制是否解除。添加定义测试场景操作和验证的子步骤。

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.添加步骤 Initialize2 来初始化组件输入。添加从 BrakeTestInitialize2 的转移。

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.添加步骤 LimitTestLimitTest 检查当车速超过高限时巡航控制是否解除。从 Initialize2 步骤添加转移,并添加子步骤来定义操作和验证。

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')

打开测试框架来查看测试序列。

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

双击 Test Sequence 模块打开编辑器并查看测试序列。

关闭测试框架和模型

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

另请参阅