MATLAB: The “runtests” function suppresses exceptions and errors thrown within TLC files

errorexceptionpassruntestssimulink codersuppresstlc

When using the MATLAB testing framework, some errors were not thrown when executing the test scripts with the command "runtests". Specifically, if the test script calls a "tlc" file that throws an error, calling "runtests" on that test script will suppress the error and the test results will pass.
A reproduction example is attached which includes three files:
  1. test.m: this is the test script
  2. mytlc.tlc: a tlc file called by test.m
  3. sub_failFunction.m: a MATLAB function that will throw an error
When running the "test.m" script normally, the error message appears. However, If you execute
>> runtests('test');
the error is no longer thrown.
 

Best Answer

The reason the errors are suppressed is that the output of the "feval" call is being recognized as text by "runtests". Making the following modifications to the test script and the "tlc" file will ensure that the error is thrown.
In "test.m":
tlc -da mytlc.tlc;
Then in "mytlc.tlc":
%assign a = FEVAL("sub_failFunction",1,2)
%assert a!=0
Using this method, an additional "assert" statement is added in the end of the "tlc" file. The test will fail as "a" is zero when "sub_failFunction" throws an error. However, note that if zero is a valid return value for "sub_failFunction", the function should be modified so that it doesn't return zero under normal circumstances e.g. zero could be mapped to some special, otherwise unused value in "sub_failFunction".