Main Content

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

sltest.testmanager.TestIteration 类

命名空间: sltest.testmanager

创建或修改测试迭代

描述

通过迭代,您可以测试模型设置的组合,以测试蒙特卡洛和参数扫描等方法。迭代在测试执行期间但在模型回调和测试回调之前初始化。一旦创建了测试迭代对象,您就可以使用类方法覆盖每次迭代的测试用例的各个方面。

您可以在测试用例的迭代部分下的文本窗口中创建迭代脚本。迭代脚本无法在 MATLAB® 命令窗口中运行。

Scripted iterations section of the Test Manager

必须将本参考页中的示例脚本插入到本节中,并且必须定义测试用例的其他部分。执行测试时,脚本迭代在模型加载之前运行。有关迭代和脚本迭代的更多信息,请参阅 测试迭代

sltest.testmanager.TestIteration 类是 handle 类。

类属性

HandleCompatible
true

有关类属性的信息,请参阅 类属性

创建对象

iterationObj = sltest.testmanager.TestIteration 返回一个测试迭代对象。该对象用于构建测试用例中的单次迭代。您想要在测试中创建的每次迭代都必须使用单个迭代对象。

您还可以使用 sltestiteration 函数在迭代脚本中创建测试迭代。

如果您在 MATLAB 命令窗口中使用 for 循环向测试用例添加许多迭代,则 MATLAB 命令窗口可能会暂时无法使用。相反,在命令窗口中使用矢量化将迭代添加到测试用例。例如:

iterations(100) = sltest.testmanager.TestIteration; 
addIteration(tc,iterations);

属性

全部展开

测试迭代的名称,指定为字符向量。迭代名称必须与测试用例中的其他迭代不同。

示例: 'Iteration 1a'

属性:

SetAccess
public
GetAccess
public
Dependent
true
NonCopyable
true

测试迭代描述文本,以字符向量形式返回。

属性:

GetAccess
public
SetAccess
public
Dependent
true
NonCopyable
true

数据类型: char

迭代的测试用例,以 sltest.testmanager.TestCase 对象形式返回。

属性:

GetAccess
public
SetAccess
private
Dependent
true
NonCopyable
true

用于迭代的模型参数覆盖集,以字符向量元胞数组形式返回。

属性:

SetAccess
protected
GetAccess
public
Dependent
true
NonCopyable
true

迭代的测试参数设置集,以字符向量元胞数组形式返回。

属性:

SetAccess
protected
GetAccess
public
Dependent
true
NonCopyable
true

用于迭代的模型变量覆盖集,以字符向量元胞数组形式返回。

属性:

SetAccess
protected
GetAccess
public
Dependent
true
NonCopyable
true

使用测试用例运行迭代的选项,指定为逻辑。

属性:

SetAccess
public
GetAccess
public
Dependent
true
NonCopyable
true

方法

全部展开

示例

全部折叠

在此脚本迭代示例中,将测试用例中的模型指定为 sldemo_absbrake。迭代是在测试执行期间生成的。这部分脚本位于测试用例的脚本模式迭代部分。它将仅在脚本模式迭代部分执行。sltest_testCase 是在脚本模式迭代部分为您定义的变量,它是迭代的父测试用例对象。

% Open the model for this example
openExample('sldemo_absbrake');

% Specify the parameter sweep
vars = 32 : 0.5 : 34;

% Create iteration for each parameter using a loop
for k = 1 : length(vars)

    % Create test iteration object
    testItr = sltest.testmanager.TestIteration;

    % Set the parameter value for this iteration
    setVariable(testItr,'Name','g','Source',...
       'base workspace','Value',vars(k));

    str = sprintf('Iteration %d',k);

    % Add the iteration object to the test case
    addIteration(sltest_testCase,testItr,str);
end

在这个脚本迭代的例子中,必须在测试用例的参数覆盖部分中定义参数集。迭代是在测试执行期间生成的。这部分脚本位于测试用例的脚本模式迭代部分。它将仅在脚本模式迭代部分执行。sltest_testCase 是在脚本模式迭代部分为您定义的变量,它是迭代的父测试用例对象。

% Define parameter sets for a test case and add this code in the

% Scripted iterations section of the test case
for k = 1 : length(sltest_parameterSets)

    % Create test iteration object    
    testItr = sltest.testmanager.TestIteration;

    % Use the parameter set in this iteration
    testItr.setTestParam('ParameterSet',sltest_parameterSets{k});
    
    str = sprintf('ParameterSet %d',k);

    % Add the iteration object to the test case
    addIteration(sltest_testCase,testItr,str);
end

备选方法

如果您不想使用脚本来创建迭代,那么您可以在测试用例中使用表迭代。有关表迭代的更多信息,请参阅 测试迭代

版本历史记录

在 R2016a 中推出