MATLAB Answers

BdS
0

How to unit test with verifyWarning?

Asked by BdS
on 30 Oct 2018
Latest activity Commented on by BdS
on 30 Oct 2018
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 Comments

Unfortunately, my unit tests show an error.
Can you post the entire error message ?
>> 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.
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.

Sign in to comment.

0 Answers