Main Content

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

sltest.testmanager.registerTestAdapter

注册适配器转换测试数据

自 R2022b 起

说明

示例

sltest.testmanager.registerTestAdapter(adapterfcn,adapterdefault) 注册一个适配器函数,将包含测试数据的 Excel® 或 MAT 文件转换为测试管理器支持的格式。有关支持的 Excel 格式的信息,请参阅 Microsoft Excel Import, Export, and Logging Format

示例

全部折叠

此示例显示如何使用和注册一个函数来转换 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 文件调整的数据和信息。例如,请参阅测试用例的 InputsIterations 部分。

Test Manager section showing checkbox to create test case from external file and filled in file and adapter fields.

Input section showing the external inputs for each worksheet in the Excel file

Iterations showing the name and description for each iteration

清理并关闭测试管理器。

sltest.testmanager.clear
sltest.testmanager.clearResults
sltest.testmanager.close

创建并注册一个函数来格式化 MAT 文件以便在测试管理器中使用。此示例假设您有一个 MAT 文件,其中包含 xyz 参数以及 xinyin 输入。

创建一个函数,转换 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 输入设置为 1true 时,即使您之前注册了不同的适配器,测试用例也会使用指定的适配器。

版本历史记录

在 R2022b 中推出