Can anyone explain the absence of functions when testing
이전 댓글 표시
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
Andy Campbell
2015년 9월 8일
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
2015년 9월 9일
채택된 답변
추가 답변 (3개)
Adam
2015년 9월 8일
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.
Simon Parten
2015년 9월 8일
편집: Walter Roberson
2020년 12월 31일
댓글 수: 4
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
2015년 9월 8일
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
2015년 9월 9일
Simon Parten
2015년 9월 9일
카테고리
도움말 센터 및 File Exchange에서 Create and Run Performance Tests에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!