Borrar filtros
Borrar filtros

How to unit test with verifyError?

35 visualizaciones (últimos 30 días)
BdS
BdS el 6 de Nov. de 2018
Comentada: Ashley Trowell el 16 de Ag. de 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.

Respuesta aceptada

Sean de Wolski
Sean de Wolski el 6 de Nov. de 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.

Más respuestas (0)

Categorías

Más información sobre Write Unit Tests en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by