Main Content

applyFixture

类: matlab.unittest.TestCase
命名空间: matlab.unittest

将脚手架与测试用例配合使用

说明

示例

applyFixture(testCase,fixture) 设置与测试用例配合使用的指定脚手架,然后在测试后拆解该脚手架。

Test 方法、TestMethodSetup 方法或 TestClassSetup 方法中使用 applyFixture。设置和拆解操作的时间和频率取决于在其中调用方法的作用域:

  • Test 方法 - applyFixture 方法为当前测试设置脚手架,并在测试运行后拆解该脚手架。

  • TestMethodSetup 方法 - applyFixture 方法在测试类中的每个测试之前设置脚手架并在该测试后拆解该脚手架。

  • TestClassSetup 方法 - applyFixture 方法为整个测试类设置一次脚手架,并在类中的所有测试运行后拆解该脚手架。

f = applyFixture(testCase,fixture) 通过将脚手架作为 matlab.unittest.fixtures.Fixture 对象返回,在设置后提供对脚手架的访问。

输入参数

全部展开

测试用例,指定为 matlab.unittest.TestCase 对象。

脚手架,指定为 matlab.unittest.fixtures.Fixture 对象。

属性

Sealedtrue

要了解方法的属性,请参阅方法属性

示例

全部展开

通过将 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 中推出