MATLAB: Error stack trace visible with verifyError

MATLABunittest

Hello all,
How can I make my test running silently when using a VerifyError which checks an error is occurring? Now it displays the entire exception trace.
My testcase is defined as:
function testcaseFHwrong(testCase)
cd(testCase.foldername);
try
testCase.verifyError(@() LoadFaultTable('Faulttablewrong.xls','SilentMode',1),'readfaulttable:NoFaultTable');
catch ME
disp(ME);
testCase.verifyTrue(False);
end
end
The function LoadFaultTable will issue an error when being run, and this error is correctly verified by the unit test framework.
The unit test is triggered using:
import matlab.unittest.TestSuite;
import matlab.unittest.TestRunner;
import matlab.unittest.plugins.TAPPlugin;
import matlab.unittest.plugins.ToFile;
suite = TestSuite.fromPackage('test_LIB_SDS_LoadFaultTable');
runner= TestRunner.withTextOutput;
tapFile = 'FHtest.tap';
plugin = TAPPlugin.producingOriginalFormat(ToFile(tapFile));
runner.addPlugin(plugin);
delete(tapFile);
result = runner.run(suite);
The command line shows the error stack trace when running this function. I did not expect this behavior
regards, Han

Best Answer

Hi Hans,
I think that there is still some key ingredient missing. I wrote the following stub for LoadFaultTable:
function LoadFaultTable(varargin)
error('readfaulttable:NoFaultTable', 'Something went wrong');
With the following surrounding test:
classdef ErrorTest < matlab.unittest.TestCase
properties
foldername = '.';
end
methods(Test)
function testcaseFHwrong(testCase)
cd(testCase.foldername);
try
testCase.verifyError(@() LoadFaultTable('Faulttablewrong.xls', ...
'SilentMode',1),'readfaulttable:NoFaultTable');
catch ME
disp(ME);
testCase.verifyTrue(False);
end
end
end
end
...and the output looks fine:
>> runtests
Running ErrorTest
.
Done ErrorTest
__________
ans =
TestResult with properties:
Name: 'ErrorTest/testcaseFHwrong'
Passed: 1
Failed: 0
Incomplete: 0
Duration: 0.011415858
Totals:
1 Passed, 0 Failed, 0 Incomplete.
0.011416 seconds testing time.
I think there is still something else at play here. Is the source code (LoadFaultTable) the thing which is printing the stack trace? Can you show the stack trace? Perhaps the call to CD removes the source and/or test from the path (here the cd is basically a no-op)?
Also, did you put the try-catch in there as a debugging step? Typically you don't need to use a try-catch when using the Throws constraint or verifyError. Taking out the try-catch produces the same expected, empty output when running the test.