Why does MATLAB's unit testing method verifyEqual fail when two numerically identical objects have different classes?

matlab, unit-testing

Solution

verifyEqual is more strict than isequal because this is usually the desired behavior when automated testing. If you are testing an algorithm that accepts an int8 and should then return an int8, providing the expected value as an int8 will catch the bug that can occur if the algorithm incorrectly returns a double. This strictness also applies to other attributes of the value such as its sparsity, complexity, and size.

In your case, if you do not care about the class of the object then you can cast the actual value to the type of the expected value such as:

verifyEqual(testCase, double(resultLogical), resultDouble);

This will result in better diagnostics than using verifyTrue. Hope that helps!

Problem

I'm using the unit testing framework in MATLAB and I noticed something weird. In this example: ``` classdef MyTest < matlab.unittest.TestCase methods (Test) function testEqual(testCase) x = [1:3 7:8]; y = 1:5; resultLogical = x == y; resultDouble = [1 1 1 0 0]; assert(isequal(resultLogical, resultDouble), 'not equal') verifyEqual(testCase, resultLogical, resultDouble) end end end ``` When I run this with `results = run(MyTest);`, the unit test fails in the `verifyEqual` statement because `resultLogical` and `resultDouble` have different classes, even though the assert statement is successful because `isequal` understands that both objects are numeric equivalents. Is there a way around this? I find myself constantly writing ``` verifyTrue(testCase, isequal(resultLogical, resultDouble)) ``` when I need to compare two arrays of different types. Is this the only way? I don't see any of the qualifications that would work better than this, unfortunately.

Original source