Lauri
Lauri

Reputation: 155

Can I configure Matlab's Unit Test Framework to fail for specific warnings?

I have a Matlab model and lots of unit tests, based on Matlab's own class-based unit test framework (matlab.unittest.TestCase and matlab.unittest.TestRunner). Tests produce quite a lot of warnings, some of which are serious from my pov. I would like the framework to report a test case failure if some of the specific warnings pop up.

A test runner can be easily configured to fail on warnings. But then it will fail on any warning:

import matlab.unittest.TestRunner;
import matlab.unittest.plugins.FailOnWarningsPlugin;
runner = TestRunner.withNoPlugins;
runner.addPlugin(FailOnWarningsPlugin);

A test runner can be also configured to ignore specific warnings, for example:

runner.addPlugin(FailOnWarningsPlugin('Ignoring',{'MATLAB:singularMatrix'}));

Here is the documentation:

https://se.mathworks.com/help/matlab/ref/matlab.unittest.plugins.failonwarningsplugin-class.html

Using 'Ignoring' flag and listing lots of warnings seems troublesome. Is there a way to do it the other way around? That is, to force my test cases to fail only on certain warnings and ignore others?

Upvotes: 1

Views: 146

Answers (1)

Anthony
Anthony

Reputation: 3793

You can temporarily set warning to be reported as errors:

s= warning('error', 'MATLAB:DELETE:FileNotFound'); % set warning as an error
warn(s) % restore the warning to non-error

Reference: https://undocumentedmatlab.com/blog/trapping-warnings-efficiently

Upvotes: 1

Related Questions