필터 지우기
필터 지우기

error stack trace visible with verifyError

조회 수: 7 (최근 30일)
Han Geerligs
Han Geerligs 2015년 2월 25일
댓글: Han Geerligs 2015년 2월 26일
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

채택된 답변

Andy Campbell
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.
  댓글 수: 1
Han Geerligs
Han Geerligs 2015년 2월 26일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Run Unit Tests에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by