Can anyone explain the absence of functions when testing
Show older comments
See the below ...
I've even gone so far as to attempt to 'addPath' to the directory which includes the convert_date.m file. I'm stumped on why I can call convert date from the command line, but not test it in the unit testing framework.
Does anyone have any ideas ?
>> convert_date('today')
ans =
2015-09-08
>> runMyTests('',false)
Running convert_date_tests
================================================================================
Error occurred in convert_date_tests/testToday and it did not run to completion.
--------------
Error Details:
--------------
Undefined function 'convert_date' for input arguments of type 'char'.
Error in convert_date_tests/testToday (line 8)
aDate = convert_date('today');
================================================================================
For bonus points on a second note, does anyone have an example of assertError?
I need that too. In a different test, the test always fails because there is an error... there should be. I should have expected assertError to not fail this test... but it always does.
2 Comments
Andy Campbell
on 8 Sep 2015
Hi Simon,
Can you show a full minimum test that is failing in this way? When I say "full minimum" I mean try to get the test down to as small as possible demonstrating the issue but its a full complete self contained test. For example, does this fail?
classdef SimpleTest < matlab.unittest.TestCase
methods(Test)
function callIt(testCase)
convert_date('today');
end
end
end
If it doesn't then perhaps there is something in your test causing this problem. Also, while we are at it, please show how you are running the test.
Simon Parten
on 9 Sep 2015
Accepted Answer
More Answers (3)
Simon Parten
on 8 Sep 2015
0 votes
Adam
on 8 Sep 2015
Not sure I understand the main part of your question, but you seem to have answered that.
As for the other part, I have done a handful of error assertions although not as many as I ought to. Here is an (slightly altered to remove confidential stuff) example of a simple one I did. If I remember correctly (and judging by the error id) this one checks for something thrown by the validateattributes function that I use all over the place. use the same method checking for my own custom errors though:
function testGetDataWithIndexVectorLongerThan2Throws( obj )
dataProvider = VolumeDataProvider3D( filename );
f = @() dataProvider.getDataByIndexRange( 3:22, [4 5], : );
obj.assertError( f, 'MATLAB:incorrectNumel' );
end
This test passes as it should by throwing the expected error caused by me passing a vector of 20 values as an argument where a vector of length 2 is expected.
1 Comment
Simon Parten
on 9 Sep 2015
Simon Parten
on 8 Sep 2015
Edited: Walter Roberson
on 31 Dec 2020
4 Comments
I would recommend testing for a specific error personally. The fact that you are writing a test asserting the error means you clearly know there should be one and therefore what its ID should be.
Testing just a generic error is dangerous because it will also catch syntax errors and simply pass the test because there was garbage code that didn't run at all!
Steven Lord
on 8 Sep 2015
Can you display the output of these two commands both in the Command Window at the prompt and inside your test, immediately before you invoke convert_date?
which -all convert_date
pwd
The reason I'm asking you to display PWD is because I suspect runMyTests is trying to run a test in a different directory. I also suspect that you're only able to call convert_date because it's in your current directory (NOT on your path) and some setup code in runMyTests changes to a different directory so convert_date is no longer in scope.
Simon Parten
on 9 Sep 2015
Simon Parten
on 9 Sep 2015
Categories
Find more on Create and Run Performance Tests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!