error stack trace visible with verifyError
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Andy Campbell
2015-2-25
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Run Unit Tests 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!