How to unit test with verifyError?

33 Ansichten (letzte 30 Tage)
BdS
BdS am 6 Nov. 2018
Kommentiert: Ashley Trowell am 16 Aug. 2021
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.

Akzeptierte Antwort

Sean de Wolski
Sean de Wolski am 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.

Weitere Antworten (0)

Kategorien

Mehr zu Write Unit Tests finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by