Reputation: 155
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
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