I am trying to verify the following function using unit testing:
function out = system_under_test(lookup)
switch lookup
case {45, 60}
out = 3;
case 154
out = 2;
case 227
out = 1;
otherwise
warning("OTHER:warning:id", "Invalid lookup value (%d) specified. Skipping.", lookup)
end
end
I am currently using the following code to test for warnings:
classdef MyTestCase < matlab.unittest.TestCase
properties (TestParameter)
lookup_ok = {45, 60, 154, 227};
lookup_expected = {3, 3, 2, 1};
lookup_warn = num2cell([1:44 46:59 61:153 155:226 228:230]);
end
methods (Test)
function ShouldWorkTest(me, lookup_ok, lookup_expected)
actual = system_under_test(lookup_ok);
me.verifyEqual(actual, lookup_expected);
end
function ShouldWarnTest(me, lookup_warn)
me.verifyWarning(system_under_test(lookup_warn), "OTHER:warning:id");
end
end
end
The ShouldWorkTest works as expected and properly satisfies the verifyEqual assertion.
The ShouldWarn test does not validate the warning is thrown but fails with the following exception although the warning is thrown as can be seen below:
Running MyTestCase
Warning: Invalid lookup value (1) specified. Skipping.
> In system_under_test (line 11)
In MyTestCase/ShouldWarnTest (line 27)
================================================================================
Error occurred in MyTestCase/ShouldWarnTest(lookup_warn=1) and it did not run to completion.
---------
Error ID:
---------
'MATLAB:unassignedOutputs'
--------------
Error Details:
--------------
Output argument "out" (and possibly others) not assigned a value in the execution with "system_under_test" function.
Error in MyTestCase/ShouldWarnTest (line 27)
me.verifyWarning(system_under_test(lookup_warn), "OTHER:warning:id");
================================================================================
I prefer not to return a value when an invalid lookup value is provided, but I do want to assert a warning is thrown. How can I do this?