MATLAB: VerifyError in a script based unit test

MATLABtestcaseunit testunittestverifyerror

Hi, my question is regarding the script-based unit testing framework. I would like to use the verifyError functionality and am struggling to generate a suitable testCase object that I can use in my script-based unit test.
For example, the run suite is called with:
import matlab.unittest.TestSuite;
import matlab.unittest.TestCase;
import matlab.unittest.TestRunner;
import matlab.unittest.plugins.TAPPlugin;
import matlab.unittest.plugins.ToFile;
suite = testsuite('unitTests','IncludingSubpackages',true);
runner = TestRunner.withTextOutput();
tapFile = fullfile(getenv('WORKSPACE'), sprintf('%s_results.tap', 'test'));
runner.addPlugin(TAPPlugin.producingOriginalFormat(ToFile(tapFile), 'LoggingLevel', 3, 'OutputDetail', 3));
results = runner.run(suite);
In the folder unitTests I would have the following script based unit test:
% Test to evaluate foo


% Initial Set-Up


a = 1;
%% Test 1. Verify Error Condition


% How can I get a test case here to call either:


% verifyError(testCase, @()foo('badArguement'), 'SomeError');


% or


% testCase.verifyError(@()foo('badArguement'), 'SomeError');


It seems that the following is not valid, as this instantiation is only for interactive use.
% Test to evaluate foo
% Initial Set-Up
a = 1;
testCase = matlab.unittest.TestCase.forInteractiveUse;
%% Test 1. Verify Error Condition
% How can I get a test case here to call either:
% verifyError(testCase, @()foo('badArguement'), 'SomeError');
% or
% testCase.verifyError(@()foo('badArguement'), 'SomeError');
or
% Test to evaluate foo
% Initial Set-Up
a = 1;
%% Test 1. Verify Error Condition
testCase = matlab.unittest.TestCase.forInteractiveUse;
% How can I get a test case here to call either:
% verifyError(testCase, @()foo('badArguement'), 'SomeError');
% or
% testCase.verifyError(@()foo('badArguement'), 'SomeError');

Best Answer

Hello,
Since there is no testCase provided for script based tests, they don't support the Qualification API, and that definitely is one reason someone might want to use function based tests instead. Can you describe some of your reasoning for using script based tests? Do you prefer them over functions or classes? This may be helpful to determine whether we should look into providing the qualification api to script based tests or if you are happy to use functions.
Thanks,
Andy
Related Question