How to use Matlab's unit test framework component verifyError to verify an error from a MEX function

조회 수: 7 (최근 30일)
Let's say I've written a MEX function, foo, that when given the number 5 throws an error.
Inside the mex function, the code would look something like:
if (inputValue == 5)
{
char errMsg[250];
sprintf_s(errMsg, "ERROR: input was %d, and so I'll throw an error", inputValue);
mexErrMsgTxt(errMsg);
}
Now, if I call foo(5), this error will be thrown.
I cannot seem to figure out how to write a unit test to verify this!
function Verify_Foo_Throws_Error(testCase)
err_msg = 'ERROR';
testCase.verifyError(@()foo(5), err_msg);
end % Verify_Foo_Throws_Error()
I cannot seem to substitute anything in for the 'err_msg' variable in my example to get the test to pass!
Interestingly enough, when err_msg has some string, the test throws the feedback:
Actual Identifier:
''
Expected Identifier:
ERROR (<- whatever the value of the variable 'err_msg' is from my example)
Yet when I make err_msg an empty string, the test fails as incomplete with the messages:
================================================================================
Uncaught error occurred in Foo_Test/Verify_Foo_Throws_Error.
The remainder of the test method will not run to completion.
--------------
Error Details:
--------------
Error using matlab.unittest.constraints.Throws (line 161)
Expected exception to be a row vector.
Error in matlab.unittest.internal.qualifications.QualificationDelegate/qualifyError (line 152)
throws = delegate.decorateConstraintAlias(Throws(errorClassOrID),'Error');
Error in matlab.unittest.qualifications.Verifiable/verifyError (line 687)
qualifyError(verifiable.VerificationDelegate, ...
Error in Foo_Test/Verify_Foo_Throws_Error (line 138)
testCase.verifyError(@()foo(5), err_msg);
================================================================================
Any assistance would be most appreciated. Thanks!

채택된 답변

Andy Campbell
Andy Campbell 2014년 6월 5일
Hi Vincent,
The verifyError function requires that the error thrown is an error with an error identifier. The identifier argument described in the verifyError documentation is not the error message but is rather this error identifier.
In order to test against this error, you'll need to change the mex file's implementation to use mexErrMsgIdAndTxt instead of mexErrMsgTxt in order to throw the error with an ID that can be tested against.
Hope that helps!
Andy
  댓글 수: 2
Vincent
Vincent 2014년 6월 5일
편집: Vincent 2014년 6월 5일
Thanks! Perhaps embarrassingly, I did not know this function existed!
That having been said, I am still getting the same result when I try the verifyError method.
If I put in the error ID (or the message text), I get back that an empty string was expected. If I put in an empty string, it throws the same errors.
EDIT: I actually implemented a simple foo.cpp file and the test for that worked fine. I'll have to dig around to find out why it's failing in my actual code. I'm sure I'm overlooking something simple. Thanks for the help!
EDIT2: Yes, I overlooked something incredibly simple (read: I'm stupid)! Thanks again for adding to my knowledge of the mex library!
Andy Campbell
Andy Campbell 2014년 6월 5일
Hey Vincent,
No need to be embarrassed, and we all overlook simple things from time to time. I am glad to have helped and good luck!
Andy

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

BdS
BdS 2018년 11월 2일
편집: Walter Roberson 2018년 11월 9일
Hi Andy,
perhaps you can help me.
how to only test the error message? I would like to test the following message written in this function:
function [CorrectFields]=FieldValidity(fieldNames,FieldDescrip)
check=FieldDescrip;
[badFld,indBadFld]=ismember('BAD_FLD',check(:,2));
if badFld
error('Bad field identifier entered: %s not found.',fieldNames{indBadFld})
else
CorrectFields=fieldNames;
end
end
Thank you in advance for your help.
  댓글 수: 3
Paul Wintz
Paul Wintz 2021년 10월 3일
In R2021a (possibly earlier) you can pass an empty string for the error ID to verifyError of errors that do not have an error ID.
Steven Lord
Steven Lord 2021년 10월 3일
If you want to verify an error that doesn't have an error identifier or where you care that an error was thrown regardless of its identifier (or even if it has an identifier) you can use a metaclass object. For the class that represents an error (like the object you would get if you wrote a try / catch to store the caught exception in a variable) the metaclass object you want to use in the verifyError call is ?MException.
try
y = ones(2) + ones(3);
catch ME
classOfME = class(ME)
idFromCaughtError = ME.identifier
end
classOfME = 'MException'
idFromCaughtError = 'MATLAB:sizeDimensionsMustMatch'
testcase = matlab.unittest.TestCase.forInteractiveUse;
verifyError(testcase, @() ones(2)+ones(3), 'MATLAB:sizeDimensionsMustMatch', ...
'Verify that a specific error was thrown')
Verification passed.
verifyError(testcase, @() ones(2)+ones(3), ?MException, ...
'Verify that an error was thrown, regardless of which one')
Verification passed.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by