error stack trace visible with verifyError

3 Ansichten (letzte 30 Tage)
Han Geerligs
Han Geerligs am 25 Feb. 2015
Kommentiert: Han Geerligs am 26 Feb. 2015
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

Akzeptierte Antwort

Andy Campbell
Andy Campbell am 25 Feb. 2015
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.
  1 Kommentar
Han Geerligs
Han Geerligs am 26 Feb. 2015
Hello Andy,
thanks for the prompt response.
I was using a try catch construction. In the catch I was issueing an error report using disp(getReport(ME)). This was causing the stack trace. Your answer got me on track to identify this issue.
regards, Han

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Run Unit Tests finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by