Catch error with assertError in test case
21 次查看(过去 30 天)
显示 更早的评论
I'd like to catch an error I am throwing with assertError in a test case. I tried doing a simple assertError, however, I get Too many output arguments.:
function fun()
error("test");
end
assertError(fun())
And if I try it in a class test suite setup it fails with the error being my error:
classdef test < matlab.unittest.TestCase
methods (Test)
function test_error(testCase)
testCase.assertError(fun());
end
end
end
3 个评论
Steven Lord
2025-3-23
assertError is part of the qualification API. See this table in the documentation; you can combine any of the "suffixes" that specify what behavior or quality you're testing for (-Equal, -Error, -Size, -Class, -Matches, etc.) with any of the "prefixes" that specify what happens if the qualification fails (assume-, verify-, assert-, fatalAssert-)
So assertError tests that the function handle throws an error (or a specific error) and if that qualification fails, it will throw an assertion. If it occurs inside a Test method, this will skip the rest of the method and move on to the next.
fatalAssertEqual tests that two things are equal and will stop all testing if it fails. [Think fatalAssert(tc, 1+1, 2) -- if that fails, there's no point in continuing to test as MATLAB is fundamentally broken.]
etc.
采纳的回答
Steven Lord
2025-3-23
Remember, you must pass a matlab.unittest.TestCase Class object into assertError as an input argument. You also need to call it with a function handle that assertError can call.
tc = matlab.unittest.TestCase.forInteractiveUse; % Make a TestCase object for experimentation
If you don't use an error ID in your error call, tell assertError that as long as some error gets thrown the test should pass.
assertError(tc, @myfunctionThatThrowsAnErrorWithoutErrorID, ?MException)
If you do use an error ID, you can specify that error ID to check for that specific error.
assertError(tc, @myfunctionThatThrowsAnErrorWithErrorID, 'MATLABAnswers:specificErrorID')
If a different error gets thrown, this test case fails. I'm using verifyError instead of assertError from this point on so that MATLAB doesn't stop executing the code, and you can see the next three test cases fail. But assertError would behave the same (except throwing a hard error instead of simply recording a test failure and moving on.)
verifyError(tc, @myfunctionThatThrowsAnErrorWithErrorID, 'MATLABAnswers:incorrectErrorID')
If no error gets thrown, the test case also fails regardless of whether you're checking for any error or a specific error.
verifyError(tc, @myfunctionThatDoesNotError, ?MException)
verifyError(tc, @myfunctionThatDoesNotError, 'MATLABAnswers:specificErrorID')
If you call the function inside assertError, MATLAB would run that function to try to use its output as the function to check for an error. Yes, you can have a function handle that returns a function handle when evaluated. In this example, when f is run it returns a function handle that will create two vectors and try to add them together.
f = @(x) @() ones(1, 5) + ones(1, x);
verifyError(tc, f(2), 'MATLAB:sizeDimensionsMustMatch') % ones(1, 5) + ones(1, 2) will error
verifyError(tc, f(10), 'MATLAB:sizeDimensionsMustMatch') % ones(1, 5) + ones(1, 10) will error
verifyError(tc, f(5), 'MATLAB:sizeDimensionsMustMatch') % ones(1, 5) + ones(1, 5) will NOT error
function myfunctionThatThrowsAnErrorWithoutErrorID()
error('This is my error')
end
function myfunctionThatThrowsAnErrorWithErrorID()
error('MATLABAnswers:specificErrorID', 'This is my error')
end
function y = myfunctionThatDoesNotError()
y = 1+1;
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function-Based Unit Tests 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!