0

我刚刚开始在 Matlab 中进行测试,我正在尝试编写一个测试来检查 inputParser 是否正确捕获了不正确的函数参数值。例如:

function [imageNamesForImport] = imageFileSearch(fileList, stringToMatch)

iP = inputParser;
iP.addRequired('fileList', @isstruct);
iP.addRequired('stringToMatch', @ischar);
iP.parse(fileList, stringToMatch);

如果我将变量作为不是结构的 fileList 传递,将引发错误

fileList = 'foo'
stringToMatch = 'bar'
imageNamesForImport = imageFileSearch(fileList, stringToMatch)

Error using imageFileSearch (line 7)
The value of 'fileList' is invalid. It must satisfy the function: isstruct.

是否可以编写单元测试来检查此输出,而不必使用一系列 try / catch 语句来为 verifyError 分配自定义错误?

4

2 回答 2

1

如果这不能回答您的问题,请参阅我的澄清问题,但您应该能够将 verifyError 与 inputParser 特定 ID 一起使用:

fileList = 'foo'
stringToMatch = 'bar'
testCase.verifyError(@() imageFileSearch(fileList, stringToMatch), ...
    'MATLAB:InputParser:ArgumentFailedValidation');

如果您想验证是否发生了更具体的错误,您可以改为使用抛出您自己的消息和 id 的函数进行验证:

function [imageNamesForImport] = imageFileSearch(fileList, stringToMatch)

iP = inputParser;
iP.addRequired('fileList', @validateStruct);
iP.addRequired('stringToMatch', @ischar);
iP.parse(fileList, stringToMatch);


function validateStruct(s)
assert(isstruct(s), 'ImageFileSearch:IncorrectInput:FileListMustBeStruct', ...
    'fileList must be a struct.'); % Can also just be inlined in addRequired call

然后您可以使用以下方法对其进行测试:

fileList = 'foo'
stringToMatch = 'bar'
testCase.verifyError(@() imageFileSearch(fileList, stringToMatch), ...
    'ImageFileSearch:IncorrectInput:FileListMustBeStruct');
于 2015-12-07T20:20:39.777 回答
0

您可以设置自己的单元测试框架并在循环try-catch中使用单个块:for

% Set up test cases
test(1).fileList = 'foo';
test(2).fileList.a = 12;
test(3).fileList.a = 'bar';

test(1).stringToMatch = 'bar';
test(2).stringToMatch = 5;
test(3).stringToMatch = 'bar';

% Run tests
myerrs = [];
for ii = 1:length(test)
    try
        imageNamesForImport = imageFileSearch(test(ii).fileList, test(ii).stringToMatch);
    catch err
        myerrs = [myerrs err];
        % Any other custom things here
    end
end

在这种情况下,这为我们提供了可以调查的 1x2 错误结构。


您还可以利用MATLAB 的单元测试框架。这是一个基于脚本的单元测试的简单示例:

图像文件搜索.m

function [imageNamesForImport] = imageFileSearch(fileList, stringToMatch)

iP = inputParser;
iP.addRequired('fileList', @isstruct);
iP.addRequired('stringToMatch', @ischar);
iP.parse(fileList, stringToMatch);
imageNamesForImport = 'hi';

测试.m

%% Test 1
fileList = 'foo';
stringToMatch = 'bar';
imageNamesForImport = imageFileSearch(fileList, stringToMatch);

%% Test 2
fileList.a = 12;
stringToMatch = 5;
imageNamesForImport = imageFileSearch(fileList, stringToMatch);

%% Test 3
fileList.a = 'bar';
stringToMatch = 'bar';
imageNamesForImport = imageFileSearch(fileList, stringToMatch);

这为我们提供了以下命令窗口输出:

Running testtrial

================================================================================
Error occurred in testtrial/Test1 and it did not run to completion.

    --------------
    Error Details:
    --------------
    The value of 'fileList' is invalid. It must satisfy the function: isstruct.

================================================================================
.
================================================================================
Error occurred in testtrial/Test2 and it did not run to completion.

    --------------
    Error Details:
    --------------
    The value of 'stringToMatch' is invalid. It must satisfy the function: ischar.

================================================================================
..
Done testtrial
__________

Failure Summary:

     Name             Failed  Incomplete  Reason(s)
    ================================================
     testtrial/Test1    X         X       Errored.
    ------------------------------------------------
     testtrial/Test2    X         X       Errored.
于 2015-12-01T23:10:20.217 回答