Main Content

matlab.unittest.fixtures.CurrentFolderFixture 类

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

用于更改当前文件夹的脚手架

描述

matlab.unittest.fixtures.CurrentFolderFixture 类提供了用于更改当前文件夹的脚手架。当测试框架设置脚手架时,脚手架将当前文件夹更改为指定的文件夹。当框架拆解脚手架时,脚手架将当前文件夹改回原始文件夹。

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

创建对象

描述

示例

fixture = matlab.unittest.fixtures.CurrentFolderFixture(folder) 创建用于将当前文件夹更改为指定文件夹的脚手架,并设置 Folder 属性。

属性

全部展开

目标文件夹的完整路径,以字符向量形式返回。在创建脚手架的过程中,将此属性的值指定为字符串标量或字符向量。您可以指定相对路径,但相对路径必须在当前文件夹中。否则,您必须指定完整路径。

如果 folder 不存在,MATLAB® 将引发错误。

属性:

GetAccess
public
SetAccess
immutable

示例

全部折叠

使用 CurrentFolderFixture 实例更改用于测试的当前文件夹。

此示例假定当前文件夹中存在文件夹 helperFiles。如果该文件夹不存在,请创建它。

[~,~] = mkdir("helperFiles")

在当前文件夹中名为 CurrentFolderTest.m 的文件中,创建 CurrentFolderTest 类,该类使用脚手架将当前文件夹更改为 helperFiles。为了简化此示例,该测试验证新的当前文件夹的完整路径是否包含子字符串 "helperFiles"

classdef CurrentFolderTest < matlab.unittest.TestCase
    methods (Test)
        function testCurrentFolder(testCase)
            import matlab.unittest.fixtures.CurrentFolderFixture
            testCase.applyFixture(CurrentFolderFixture("helperFiles"))
            testCase.verifySubstring(pwd,"helperFiles")
        end
    end
end

在运行 CurrentFolderTest 类之前,首先返回当前文件夹的路径。返回的路径不包含 "helperFiles"

pwd
ans =

    'C:\work'

运行测试类。脚手架将当前文件夹更改为 helperFiles。因此,测试通过。

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

一旦测试运行完毕,测试框架就会拆解脚手架,环境还原到其原始状态。

pwd
ans =

    'C:\work'

版本历史记录

在 R2013b 中推出