sltest.testmanager.registerTestAdapter
说明
sltest.testmanager.registerTestAdapter( 注册一个适配器函数,将包含测试数据的 Excel® 或 MAT 文件转换为测试管理器支持的格式。有关支持的 Excel 格式的信息,请参阅 Microsoft Excel 导入、导出和记录格式。adapterfcn,adapterdefault)
示例
此示例显示如何使用和注册一个函数来转换 Excel 电子表格的格式,以便可以在测试管理器中使用。该电子表格有 6 张表,每张表对应一次迭代。每张表都有一个标题,其中包含迭代名称和描述。该表还包括用于指定测试输入、参数和基线的内置格式。此示例使用 CruiseContol 模型。
本例中使用的文件是:
CruiseControlTests_CustomHeader.xlsx- Excel 电子表格HybridBuiltInAdapter.m- 适配器功能CruiseControlTests_HybridBuiltInAdapter.mldatx- 测试文件
查看适配器功能。
type HybridBuiltInAdapter.mfunction HybridBuiltInAdapter(testCase, inputFileName)
% Find iteration test specs (worksheets)
[sheets] = sheetnames(inputFileName);
% Configure test case from each worksheet
for ii=1:numel(sheets)
% Create new iteration object and get iteration information
testItr = sltestiteration();
[testItr.Name, testItr.Description,specRange] = ...
getTestInfo(inputFileName,sheets{ii});
% Add a single input set from worksheet and map to ports
testInput = testCase.addInput(inputFileName,Sheet=sheets(ii),...
Ranges={specRange},SimulationIndex=1,CreateIterations=false);
testInput.map('Mode',0,'CompileModel',true);
setTestParam(testItr,'ExternalInput',testInput.Name);
% Add a single baseline set from worksheet
baseline = testCase.addBaselineCriteria(inputFileName, ...
Sheet=sheets(ii),Ranges={specRange});
setTestParam(testItr,'Baseline',baseline.Name);
% Add iteration to run in this test case
addIteration(testCase,testItr);
end
end
function [name,desc,specRange] = getTestInfo(inputFileName,sheetName)
% Get test name and description, input/output cell range, and
% parameter data from worksheet
rawT = readcell(inputFileName,'Sheet',sheetName,'UseExcel',false);
% Assign missing data to '' for "isempty" checks
rawT(cellfun(@(x) all(ismissing(x)), rawT)) = {''};
% Find row with "Name" and find row with "Description"
nameRow = find(cellfun(@(x) strcmpi(x,'Name'),rawT(:,1)),1);
name = rawT{nameRow,2};
descRow = find(cellfun(@(x) strcmpi(x,'Description'),rawT(:,1)),1);
desc = rawT{descRow,2};
% Find test vector row range
timeRow = find(cellfun(@(x) strcmpi(x,'Time'), rawT(:,1)));
endRow = numel(rawT(:,1));
specRange = [num2str(timeRow) ':' num2str(endRow)];
end
注册适配器。
sltest.testmanager.registerTestAdapter... ('HybridBuiltInAdapter',true);
在测试管理器中打开 CruiseControlTests_HybridBuiltInAdapter 测试文件。
tf = sltest.testmanager.TestFile... ('CruiseControlTests_HybridBuiltInAdapter.mldatx'); sltest.testmanager.view
在测试管理器中选择从外部文件创建测试用例并指定文件和适配器,即可添加从 Excel 文件调整的数据和信息。例如,请参阅测试用例的输入和迭代部分。



清理并关闭测试管理器。
sltest.testmanager.clear sltest.testmanager.clearResults sltest.testmanager.close
创建并注册一个函数来格式化 MAT 文件以便在测试管理器中使用。此示例假设您有一个 MAT 文件,其中包含 x、y 和 z 参数以及 xin 和 yin 输入。
创建一个函数,转换 MAT 文件中的基线测试参数集。
function Adapter_InputBaselineParamSet(test,externalMATFile) load(externalMATFile); % Set model property for the test case setProperty(test,'Model',mdl_name); % Save external file parameters to a supported format % MAT file and add parameter set to test case save('param.mat','x','y','z'); paramset = addParameterSet(test,'FilePath',... 'param.mat','SimulationIndex',1) % Save external file inputs to a supported format MAT % file and add inputs to test case save('input.mat','xin','yin'); inputs = addInput(test,'input.mat'); inputs.map(3); % Capture baseline criteria and add to test case captureBaselineCriteria(test,'base.mat',true);
然后使用其函数句柄注册该函数。
sltest.testmanager.registerTestAdapter(...
@Adapter_InputBaselineParamSet,true); 输入参数
适配器函数名称,指定为函数句柄。适配器功能指定如何将 Excel 或 MAT 测试数据文件转换为测试管理器支持的格式。
是否使用指定的适配器作为测试用例的默认适配器,指定为数字或逻辑 1 (true) 或 0 (false)。当您将 adapterdefault 输入设置为 1 或 true 时,即使您之前注册了不同的适配器,测试用例也会使用指定的适配器。
版本历史记录
在 R2022b 中推出
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)