How to unit test with verifyError?

32 views (last 30 days)
Hi everyone,
I am trying to unit test the following function:
function res=add5(x) % ADD5 Increment input by 5.
if ~isa(x,'numeric')
error('add5:InputMustBeNumeric','Input must be numeric.')
end
res=x+5;
end
I run the unit test with the following function:
function testadd5(testCase)
actSolution=add5('c');
verifyError(testCase,actSolution,'add5:InputMustBeNumeric')
end
As a test result I get the following message:
Verification failed in getBbgFieldDataTest/testadd5.
Error occurred in getBbgFieldDataTest/testadd5 and it did not run to completion.
---------
Error ID:
---------
'add5:InputMustBeNumeric'
--------------
Error Details:
--------------
Error using add5 (line 4)
Input must be numeric.
Error in getBbgFieldDataTest>testadd5 (line 67)
actSolution=add5('c');
I would appreciate your support. Thank you in advance.

Accepted Answer

Sean de Wolski
Sean de Wolski on 6 Nov 2018
verifyError(testCase, @()add5('c'), 'add5:InputMustBeNumeric')
Pass the function handle into verifyError rather than a result. Verify Error will then call the function itself.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!