Main Content

matlab.unittest.fixtures.WorkingFolderFixture 类

命名空间: matlab.unittest.fixtures

用于创建和更改为临时工作文件夹的脚手架

描述

matlab.unittest.fixtures.WorkingFolderFixture 创建一个临时文件夹并将其设置为当前工作文件夹。测试本身或待测产品会创建文件并修改文件夹的内容,但不会影响源代码或测试文件夹的结构。

当测试框架设置脚手架时,会将当前文件夹添加到路径中。接下来,脚手架会创建一个临时文件夹,并将当前工作文件夹更改为临时文件夹。当测试框架拆解脚手架时,默认情况下,会删除临时文件夹以及所有文件夹的内容。测试框架会将当前工作文件夹恢复为其以前的状态。

WorkingFolderFixtureTemporaryFolderFixture 脚手架都创建一个临时文件夹。但是,WorkingFolderFixture 还会将该文件夹设置为当前工作文件夹。

构造

matlab.unittest.fixtures.WorkingFolderFixture 将构建一个脚手架,用于创建和更改为临时工作文件夹。

matlab.unittest.fixtures.WorkingFolderFixture(Name,Value) 使用由一个或多个 Name,Value 对组参量指定的其他选项构造一个脚手架。例如,matlab.unittest.fixtures.WorkingFolderFixture('PreservingOnFailure',true) 会构建一个脚手架,在发生错误时该脚手架不会删除临时文件夹。

输入参量

全部展开

名称-值参数

将可选的参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但参量对组的顺序无关紧要。

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

在测试失败后保留临时文件夹及其内容的设置,指定为 falsetruelogical 01)。默认值为 false。在构建套件期间您可以将其指定为 true

如果将 'PreservingOnFailure' 指定为 true 且使用脚手架的测试失败,则测试框架将在命令行窗口中显示一条消息,但不会删除文件夹。失败的情况包括使用脚手架的测试中发生鉴定、断言或致命断言鉴定失败以及未捕获的错误。保留临时文件夹及其内容有助于调查测试失败的原因。

数据类型: logical

临时文件夹名称的后缀,指定为字符向量。此参数的值附加到临时文件夹的名称后面。

示例: WorkingFolderFixture('WithSuffix','_ProductA')

属性

全部展开

脚手架所创建的文件夹的绝对路径,指定为字符向量。

表明在测试失败后是否保留临时文件夹及其内容的指示符,指定为 falsetrue。此属性由构造函数通过名称-值对组参量 'PreservingOnFailure' 设置。

临时文件夹名称的后缀,指定为字符向量。此属性由构造函数通过名称-值对组参量 'WithSuffix' 设置。

复制语义

句柄。要了解句柄类如何影响复制操作,请参阅复制对象

示例

全部折叠

在 MATLAB® 路径中创建以下 ExampleTest 类定义。

classdef ExampleTest < matlab.unittest.TestCase
    methods(Test)
        function test1(testCase)
            import matlab.unittest.fixtures.WorkingFolderFixture;
            
            testCase.applyFixture(WorkingFolderFixture);
            
            x = 1:10;
            
            % Save a file in the temporary folder
            save('data.mat','x');
            
            disp(['The temporary working folder: ' pwd])
            ls
        end
    end
end

在命令提示符下运行测试。

run(ExampleTest);
Running ExampleTest
The temporary working folder: C:\AppData\Local\Temp\tp6ff2cadf_9eed_4e90_88c1_5ff9ee8abb25

.         ..        data.mat  

.
Done ExampleTest
__________

临时文件夹的名称有所不同。

在 MATLAB 路径中创建以下 ExampleTest2 类定义。

classdef ExampleTest2 < matlab.unittest.TestCase
    methods(Test)
        function test1(testCase)
            import matlab.unittest.fixtures.WorkingFolderFixture;
            
            f = WorkingFolderFixture('WithSuffix','_ProductA');
            testCase.applyFixture(f);
            
            x = 1:10;
            
            % Save a file in the temporary folder
            save('data.mat','x');
            
            disp(['The temporary working folder: ' pwd])
            ls
        end
    end
end

在命令提示符下运行测试。

run(ExampleTest2);
Running ExampleTest2
The temporary working folder: C:\AppData\Local\Temp\tp72c6ce7c_a380_4f5e_be3b_4f7191a6cd2c_ProductA

.         ..        data.mat  

.
Done ExampleTest2
__________

临时文件夹的名称有所不同,但都以 _ProductA 结尾。

版本历史记录

在 R2016a 中推出