sltest.testmanager.registerTestAdapter
说明
sltest.testmanager.registerTestAdapter(
注册一个适配器函数,将包含测试数据的 Excel® 或 MAT 文件转换为测试管理器支持的格式。有关支持的 Excel 格式的信息,请参阅 Microsoft Excel Import, Export, and Logging Format。adapterfcn
,adapterdefault
)
示例
注册 Excel 适配器
此示例显示如何使用和注册一个函数来转换 Excel 电子表格的格式,以便可以在测试管理器中使用。该电子表格有 6 张表,每张表对应一次迭代。每张表都有一个标题,其中包含迭代名称和描述。该表还包括用于指定测试输入、参数和基线的内置格式。此示例使用 CruiseContol
模型。
本例中使用的文件是:
CruiseControlTests_CustomHeader.xlsx
- Excel 电子表格HybridBuiltInAdapter.m
——适配器功能CruiseControlTests_HybridBuiltInAdapter.mldatx
——测试文件
查看适配器功能。
type HybridBuiltInAdapter.m
function 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 文件调整的数据和信息。例如,请参阅测试用例的 Inputs 和 Iterations 部分。
清理并关闭测试管理器。
sltest.testmanager.clear sltest.testmanager.clearResults sltest.testmanager.close
在 MAT 文件中注册基线参数
创建并注册一个函数来格式化 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);
输入参数
adapterfcn
— 适配器函数名称
函数句柄
适配器函数名称,指定为函数句柄。适配器功能指定如何将 Excel 或 MAT 测试数据文件转换为测试管理器支持的格式。
adapterdefault
— 是否使用适配器作为默认
false
或 0
(默认) | true
或 1
是否使用指定的适配器作为测试用例的默认适配器,指定为数字或逻辑 1
(true
) 或 0
(false
)。当您将 adapterdefault
输入设置为 1
或 true
时,即使您之前注册了不同的适配器,测试用例也会使用指定的适配器。
版本历史记录
在 R2022b 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)