Problems using type double in fatalAssertEqual function (unit testing)

9 views (last 30 days)
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.

Answers (1)

Andy Campbell
Andy Campbell on 12 Jun 2015
Edited: Andy Campbell on 12 Jun 2015
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));
However, in scripts you don't have any testCase variable to work with. If you are interested in this take a look at function based testing overview and example (and another).
  1 Comment
Andy Campbell
Andy Campbell on 12 Jun 2015
Edited: Andy Campbell on 12 Jun 2015
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!