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 个评论
James
James 2025-3-23
编辑:James 2025-3-23
Interesting. I found verifyError here, but I have the same issues with that instead of testError. Maybe there is no way to check if a function raises an error?
Steven Lord
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
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)
Assertion passed.
If you do use an error ID, you can specify that error ID to check for that specific error.
assertError(tc, @myfunctionThatThrowsAnErrorWithErrorID, 'MATLABAnswers:specificErrorID')
Assertion passed.
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')
Verification failed. --------------------- Framework Diagnostic: --------------------- verifyError failed. --> The function threw the wrong exception. Actual Exception: 'MATLABAnswers:specificErrorID' Expected Exception: 'MATLABAnswers:incorrectErrorID' --> Actual Error Report: Error using LiveEditorEvaluationHelperEeditorId>myfunctionThatThrowsAnErrorWithErrorID (line 16) This is my error Evaluated Function: function_handle with value: @myfunctionThatThrowsAnErrorWithErrorID ------------------ Stack Information: ------------------ In /tmp/Editor_hgeli/LiveEditorEvaluationHelperEeditorId.m (LiveEditorEvaluationHelperEeditorId) at 4 In /MATLAB/toolbox/matlab/connector2/interpreter/+connector/+internal/fevalMatlab.p (fevalMatlab) at 0 In /MATLAB/toolbox/matlab/connector2/interpreter/+connector/+internal/fevalJSON.p (fevalJSON) at 0
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)
Verification failed. --------------------- Framework Diagnostic: --------------------- verifyError failed. --> The function did not throw any exception. Expected Exception: ?MException Evaluated Function: function_handle with value: @myfunctionThatDoesNotError ------------------ Stack Information: ------------------ In /tmp/Editor_hgeli/LiveEditorEvaluationHelperEeditorId.m (LiveEditorEvaluationHelperEeditorId) at 5 In /MATLAB/toolbox/matlab/connector2/interpreter/+connector/+internal/fevalMatlab.p (fevalMatlab) at 0 In /MATLAB/toolbox/matlab/connector2/interpreter/+connector/+internal/fevalJSON.p (fevalJSON) at 0
verifyError(tc, @myfunctionThatDoesNotError, 'MATLABAnswers:specificErrorID')
Verification failed. --------------------- Framework Diagnostic: --------------------- verifyError failed. --> The function did not throw any exception. Expected Exception: 'MATLABAnswers:specificErrorID' Evaluated Function: function_handle with value: @myfunctionThatDoesNotError ------------------ Stack Information: ------------------ In /tmp/Editor_hgeli/LiveEditorEvaluationHelperEeditorId.m (LiveEditorEvaluationHelperEeditorId) at 6 In /MATLAB/toolbox/matlab/connector2/interpreter/+connector/+internal/fevalMatlab.p (fevalMatlab) at 0 In /MATLAB/toolbox/matlab/connector2/interpreter/+connector/+internal/fevalJSON.p (fevalJSON) at 0
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
Verification passed.
verifyError(tc, f(10), 'MATLAB:sizeDimensionsMustMatch') % ones(1, 5) + ones(1, 10) will error
Verification passed.
verifyError(tc, f(5), 'MATLAB:sizeDimensionsMustMatch') % ones(1, 5) + ones(1, 5) will NOT error
Verification failed. --------------------- Framework Diagnostic: --------------------- verifyError failed. --> The function did not throw any exception. Expected Exception: 'MATLAB:sizeDimensionsMustMatch' Evaluated Function: function_handle with value: @()ones(1,5)+ones(1,x) ------------------ Stack Information: ------------------ In /tmp/Editor_hgeli/LiveEditorEvaluationHelperEeditorId.m (LiveEditorEvaluationHelperEeditorId) at 10 In /MATLAB/toolbox/matlab/connector2/interpreter/+connector/+internal/fevalMatlab.p (fevalMatlab) at 0 In /MATLAB/toolbox/matlab/connector2/interpreter/+connector/+internal/fevalJSON.p (fevalJSON) at 0
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 CenterFile Exchange 中查找有关 Function-Based Unit Tests 的更多信息

标签

产品


版本

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by