How to unit test with verifyWarning?

조회 수: 1 (최근 30일)
BdS
BdS 2018년 10월 30일
댓글: BdS 2018년 10월 30일
Hello, I am unit testing the following function:
function [adjdate] = AdjStartDate(startDate) if ~strcmp(startDate,datestr(datetime('01-Feb-2000','InputFormat','dd-MMM-yyyy'),'yyyy-mm-dd')) adjdate=datestr(datetime('01-Feb-2000','InputFormat','dd-MMM-yyyy'),'yyyy-mm-dd'); warning('Start date has been modified.') else adjdate=startDate; end
Specifically, I want to unit test if the function gives the warning 'Start date has been modified." if the date is changed. I tried the following code:
% test if warning is shown, if input date changes function testWarning(testCase) actSolution=AdjStartDate('2000-01-01'); verifiableWaning(testCase,actSolution) end
Unfortunately, my unit tests show an error. There is something written about the warning ID, but I did not specified any warning ID in the function AdjStartDate.m Would you give me some assistance?
  댓글 수: 3
BdS
BdS 2018년 10월 30일
>> results=runtests('getBbgFieldDataTest') Running getBbgFieldDataTest Warning: Start date has been modified. > In AdjStartDate (line 14) In getBbgFieldDataTest>testDateValue (line 12) ..Warning: Start date has been modified. > In AdjStartDate (line 14) In getBbgFieldDataTest>testWarning (line 26)
================================================================================ Error occurred in getBbgFieldDataTest/testWarning and it did not run to completion.
---------
Error ID:
---------
'MATLAB:UndefinedFunction'
--------------
Error Details:
--------------
Undefined function 'verifiableWarning' for input arguments of type 'matlab.unittest.FunctionTestCase'.
Error in getBbgFieldDataTest>testWarning (line 27)
verifiableWarning(testCase,actSolution)
================================================================================
.
Done getBbgFieldDataTest
__________
Failure Summary:
Name Failed Incomplete Reason(s)
================================================================
getBbgFieldDataTest/testWarning X X Errored.
results =
1×3 TestResult array with properties:
Name
Passed
Failed
Incomplete
Duration
Details
Totals: 2 Passed, 1 Failed (rerun), 1 Incomplete. 0.016652 seconds testing time.
BdS
BdS 2018년 10월 30일
I changed the function testWarning.m Unfortunately the test is still negative.
% test if warning is shown, if input date changes function testWarning(testCase) actSolution=AdjStartDate('2000-01-01'); verifyWarning(testCase,actSolution,[]) end
Output:
>> results=runtests('getBbgFieldDataTest') Running getBbgFieldDataTest Warning: Start date has been modified. > In AdjStartDate (line 14) In getBbgFieldDataTest>testDateValue (line 12) ..Warning: Start date has been modified. > In AdjStartDate (line 14) In getBbgFieldDataTest>testWarning (line 26)
================================================================================ Error occurred in getBbgFieldDataTest/testWarning and it did not run to completion.
---------
Error ID:
---------
'MATLAB:invalidType'
--------------
Error Details:
--------------
Error using matlab.unittest.internal.mustBeTextScalar (line 11)
Expected warningID to be one of these types:
char, string
Instead its type was double.
Error in matlab.unittest.internal.qualifications.QualificationDelegate/qualifyWarning (line 192)
mustBeTextScalar(warningID,'warningID');
Error in matlab.unittest.qualifications.Verifiable/verifyWarning (line 754)
[varargout{1:nargout}] = qualifyWarning(verifiable.VerificationDelegate, ...
Error in getBbgFieldDataTest>testWarning (line 27)
verifyWarning(testCase,actSolution,[])
================================================================================
.
Done getBbgFieldDataTest
__________
Failure Summary:
Name Failed Incomplete Reason(s)
================================================================
getBbgFieldDataTest/testWarning X X Errored.
results =
1×3 TestResult array with properties:
Name
Passed
Failed
Incomplete
Duration
Details
Totals: 2 Passed, 1 Failed (rerun), 1 Incomplete. 0.018611 seconds testing time.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by