是否可以将Matlab的单元测试框架配置为因特定警告而失败?

时间:2019-04-16 10:53:50

标签: matlab

基于Matlab自己的基于类的单元测试框架(matlab.unittest.TestCase和matlab.unittest.TestRunner),我有一个Matlab模型和许多单元测试。测试会产生很多警告,其中一些警告在我看来很严重。如果某些特定警告弹出,我希望框架报告测试用例失败。

可以轻松配置测试运行器,使其在出现警告时失败。但是,如果出现任何警告,它将失败:

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

也可以将测试运行器配置为忽略特定的警告,例如:

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

以下是文档:

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

使用“忽略”标志并列出许多警告似乎很麻烦。 有其他方法可以做到吗?也就是说,要强制我的测试用例仅在某些警告下失败而忽略其他警告?

1 个答案:

答案 0 :(得分:1)

您可以临时设置警告以报告为错误:

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

参考:https://undocumentedmatlab.com/blog/trapping-warnings-efficiently