inputParser输出matlab的单元测试

unit test for inputParser output matlab

我刚刚开始在 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 分配自定义错误?

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

% 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's Unit Testing Framework. Here's a simple example for a Script-Based Unit Test:

imageFileSearch.m

function [imageNamesForImport] = imageFileSearch(fileList, stringToMatch)

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

testtrial.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);

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

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.

如果这不能回答您的问题,请参阅我的澄清问题,但您应该能够将 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');