Problems using type double in fatalAssertEqual function (unit testing)
조회 수: 3 (최근 30일)
이전 댓글 표시
I'm trying to learn unit testing by writing some simple functions and performing unit tests. Here is the function I wrote:
% CalcDotProduct
% ------------------------------------------ %
% Calculates dot product between two vectors
function product = CalcDotProduct(vec1,vec2)
product = 0;
for i = 1:length(vec1)
product = product + vec1(i)*vec2(i);
end
end
Here is the script-based unit test I use for the function:
% test vectors
a = [1 2 3];
b = [1 1 1];
c = [1 2];
% preconditions
fatalAssertEqual(length(a),length(b));
fatalAssertEqual(length(a),length(c));
fatalAssertEqual(length(b),length(c));
%%Test 1
expVal = 6;
actVal = CalcDotProduct(a,b);
verifyEqual(testCase,expVal,actVal);
Here is how I call the test and the error I get:
>> result = runtests('CalcDotProductTest')
Running CalcDotProductTest
================================================================================
Error occurred while setting up or tearing down CalcDotProductTest.
As a result, all CalcDotProductTest tests failed and did not run to completion.
--------------
Error Details:
--------------
Undefined function 'fatalAssertEqual' for input arguments of type 'double'.
================================================================================
Why is this happening? My understanding of the fatalAssertTrue function is that it takes in two values to see if they are equal and stops the rest of the script if they are not. I'm not sure why type double would be an invalid input for this comparison.
댓글 수: 0
답변 (1개)
Andy Campbell
2015년 6월 12일
편집: Andy Campbell
2015년 6월 12일
Hello,
Learning about unit testing is definitely a good thing, and I'd be happy to help as you encounter questions.
The use of the qualification API functions (like verifyTrue, verifyEqual, fatalAssertLength, etc basically everything here) are not available in script based testing. In script based testing you only have access to the assert function and you have to code the qualification logic in the test itself.
If this seems like a significant limitation of script based testing, you are right, it cerainly is! However, script based testing is helpful to get testing quickly for simple tests where you don't need these features, and you don't yet have to learn more sophisticated APIs.
The next step up from script based testing is function based testing, and while it requires you to write a structured function one of the major benefits you get is the ability to use the qualification API. This is because in this context you are handed a testCase and you use that testCase in the the verify/fatalAssert/etc functions. IN your code above it actually needs to be:
fatalAssertEqual(testCase, length(a),length(b));
or even better
fatalAssertLength(testCase, a,length(b));
댓글 수: 1
Andy Campbell
2015년 6월 12일
편집: Andy Campbell
2015년 6월 12일
Also, from your description it sounds like you actually may want to use assertEqual/assertLength rather than fatalAssert...the difference is fatal assertions abort the entire testing session when they fail, while assertEqual/etc fail the test but continues the test session and returns a TestResult.
참고 항목
카테고리
Help Center 및 File Exchange에서 Write Unit Tests에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!