필터 지우기
필터 지우기

How to unit test with verifyError?

조회 수: 35 (최근 30일)
BdS
BdS 2018년 11월 6일
댓글: Ashley Trowell 2021년 8월 16일
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.

채택된 답변

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Write Unit Tests에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by