Effacer les filtres
Effacer les filtres

How to unit test with verifyError?

35 vues (au cours des 30 derniers jours)
BdS
BdS le 6 Nov 2018
Commenté : Ashley Trowell le 16 Août 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.

Réponse acceptée

Sean de Wolski
Sean de Wolski le 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.
  1 commentaire
Ashley Trowell
Ashley Trowell le 16 Août 2021
Thanks a bunch!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Write Unit Tests dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by