我想测试一个抛出错误的类但是我使用Matlab 2011b而我找不到matlab.unittest(要使用matlab.unittest.TestSuite.fromFile)。
我可以使用什么?
答案 0 :(得分:1)
一种方法是将其写为script based test。这种方式在升级时,它将在新版本中与测试框架一起开箱即用。与此同时,您可以通过调用脚本来运行测试。
如果您现在无法升级,那么您可以编写类似以下帮助函数的内容来测试脚本中的这些错误:
function assertError(fcn, errorID)
e = MException.empty;
try
fcn();
catch e
end
assert(~isempty(e), 'No error occurred. Expected an error with the id "%s"', errorID);
assert(strcmp(e.identifier, errorID), ...
'Wrong error occurred. Expected id "%s", but id "%s" was thrown.', ...
errorID, e.identifier);
要测试一下:
>> assertError(@()error('some:id','Some message'), 'some:id') % no failure
>> assertError(@()disp(5), 'some:id')
5
Error using assertError (line 8)
No error occured. Expected an error with the id "some:id"
>> assertError(@()error('other:id','Some message'), 'some:id')
Error using assertError (line 9)
Wrong error occurred. Expected id "some:id", but id "other:id" was thrown.
>>