Matlab unittests中多个异常之一的测试
我使用Matlab的unittest来测试无效参数的处理 在考试中我有一句台词Matlab unittests中多个异常之一的测试,matlab,unit-testing,exception-handling,Matlab,Unit Testing,Exception Handling,我使用Matlab的unittest来测试无效参数的处理 在考试中我有一句台词 t.verifyError(@myObject.myMethod, 'MATLAB:nonStrucReference'); 在Matlab R2014a中工作正常,但在Matlab R2016a中失败,并显示以下消息 --------------------- Framework Diagnostic: --------------------- verifyError failed. --> The fu
t.verifyError(@myObject.myMethod, 'MATLAB:nonStrucReference');
在Matlab R2014a中工作正常,但在Matlab R2016a中失败,并显示以下消息
---------------------
Framework Diagnostic:
---------------------
verifyError failed.
--> The function threw the wrong exception.
Actual Exception:
'MATLAB:structRefFromNonStruct'
Expected Exception:
'MATLAB:nonStrucReference'
我想知道是否有可能测试是否抛出了一个异常
我知道这是可以写的
t.verifyError(@myObject.myMethod, ?MException);
但是更具体的方法会更好。您可能希望编写一个自定义验证方法,该方法接受异常单元格数组作为输入
function verifyOneOfErrors(testcase, func, identifiers, varargin)
% Ensure that a cell array was passed rather than a string
if ischar(identifiers)
identifiers = {identifiers};
end
% If the function succeeds with no errors, then we want a failure
threw_correct_error = false;
try
func()
catch ME
% Check if the identifier is in our list of approved identifiers
threw_correct_error = ismember(ME.identifier, identifiers);
end
% Do the actual verification
testcase.verifyTrue(threw_correct_error, varargin{:})
end
另一种方法是通过显式地引起错误并检索标识符,在测试用例中动态地获取错误消息标识符
% Get a version-specific identifier for this specific error
try; a = []; a.field; catch ME; end;
% Verify that your method throws this error
t.verifyError(@myObject.myMethod, ME.identifier)