I am just beginning to dive into testing in Matlab, and I am trying to write a test which will check whether inputParser is properly catching incorrect function argument values. For example:
function [imageNamesForImport] = imageFileSearch(fileList, stringToMatch)
iP = inputParser;
iP.addRequired('fileList', @isstruct);
iP.addRequired('stringToMatch', @ischar);
iP.parse(fileList, stringToMatch);
will throw an error if I pass a variable as fileList which is not a structure
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.
Is it possible to write a unit test to check for this output without having to use a series of try / catch statements to assign custom errors for verifyError?
You could set up your own unit testing framework and use a single try-catch
block inside of a for
loop:
% 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
Which in this case gives us a 1x2 structure of errors that we can investigate.
You could also utilize 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);
Which gives us the following command window output:
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.