Main Content

matlab.unittest.fixtures.SuppressedWarningsFixture 类

命名空间: matlab.unittest.fixtures
超类: matlab.unittest.fixtures.Fixture

用于隐藏警告显示的脚手架

描述

matlab.unittest.fixtures.SuppressedWarningsFixture 类提供用于隐藏警告显示的脚手架。当测试框架设置脚手架时,脚手架会隐藏指定的警告。当框架拆解脚手架时,脚手架将警告的状态将还原为其原始值。

matlab.unittest.fixtures.SuppressedWarningsFixture 类是一个 handle 类。

创建对象

描述

示例

fixture = matlab.unittest.fixtures.SuppressedWarningsFixture(warnings) 创建一个用于隐藏指定警告显示的脚手架并设置 Warnings 属性。

属性

全部展开

要隐藏的警告的标识符,以字符向量元胞数组形式返回。在创建脚手架的过程中,将此属性的值指定为字符串数组、字符向量或字符向量元胞数组。

示例: {'MATLAB:MKDIR:DirectoryExists'}

示例: {'MyComponent:FirstID','MyComponent:SecondID'}

属性:

GetAccess
public
SetAccess
private

示例

全部折叠

通过将 SuppressedWarningsFixture 实例与测试用例一起使用,可以在运行测试时隐藏警告。例如,隐藏在尝试从搜索路径中删除不存在的文件夹时发出的警告。

当您使用 rmpath 命令从路径中删除不存在的文件夹时,该命令会发出警告。

rmpath nonexistentFolder
Warning: "nonexistentFolder" not found in path.

返回由 rmpath 命令发出的警告的标识符。

[~,identifier] = lastwarn
identifier =

    'MATLAB:rmpath:DirNotFound'

在当前文件夹中名为 SuppressedWarningTest.m 的文件中,创建一个测试类,该类验证调用 rmpath 运行时不显示任何警告。要使测试通过,请在测试中调用 applyFixture 方法。

classdef SuppressedWarningTest < matlab.unittest.TestCase
    methods (Test)
        function testWarningFree(testCase)
            import matlab.unittest.fixtures.SuppressedWarningsFixture
            testCase.applyFixture( ...
                SuppressedWarningsFixture("MATLAB:rmpath:DirNotFound"))
            testCase.verifyWarningFree(@() rmpath("nonexistentFolder"))
        end
    end
end

运行测试类。当 Test 方法调用函数句柄时,脚手架会隐藏指定的警告。测试通过。

runtests("SuppressedWarningTest");
Running SuppressedWarningTest
.
Done SuppressedWarningTest
__________

一旦测试运行完毕,测试框架就会拆解脚手架,环境还原到其原始状态。因此,用不存在的文件夹对 rmpath 进行新调用会导致警告。

rmpath nonexistentFolder
Warning: "nonexistentFolder" not found in path.

版本历史记录

在 R2013b 中推出