Main Content

matlab.unittest.fixtures.TemporaryFolderFixture 类

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

用于创建临时文件夹的脚手架

描述

matlab.unittest.fixtures.TemporaryFolderFixture 类提供用于创建临时文件夹的脚手架。当测试框架设置脚手架时,脚手架会创建一个临时文件夹。当框架拆解脚手架时,脚手架会删除该文件夹及其内容。在删除文件夹之前,脚手架首先从内存中清除在临时文件夹中定义的所有函数、MEX 文件和类。

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

创建对象

描述

示例

fixture = matlab.unittest.fixtures.TemporaryFolderFixture 构造用于创建临时文件夹的套件。

示例

fixture = matlab.unittest.fixtures.TemporaryFolderFixture(Name,Value) 使用一个或多个名称-值参量设置其他选项。例如,fixture = matlab.unittest.fixtures.TemporaryFolderFixture("WithSuffix","_FeatureA") 会构造一个脚手架,该脚手架会创建一个临时文件夹,该临时文件夹名称具有指定的后缀。

输入参量

全部展开

名称-值参数

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

示例: fixture = matlab.unittest.fixtures.TemporaryFolderFixture(WithSuffix="_FeatureA")

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

示例: fixture = matlab.unittest.fixtures.TemporaryFolderFixture("WithSuffix","_FeatureA")

测试失败后是否保留临时文件夹及其内容,指定为数值或逻辑值 0 (false) 或 1 (true)。失败的情况包括使用脚手架的测试中发生验证、断言或致命断言失败以及未捕获的错误。

默认情况下,当框架遇到测试失败时,它会拆解脚手架,该脚手架会删除临时文件夹及其内容。如果将值指定为 true,则脚手架在失败后不会删除临时文件夹及其内容。保留临时文件夹及其内容有助于调试失败的原因。

此参量设置 PreserveOnFailure 属性。

临时文件夹名称的后缀,指定为字符串标量或字符向量。

此参量设置 Suffix 属性。

属性

全部展开

脚手架在设置过程中创建的临时文件夹的完整路径,以字符向量形式返回。当框架设置脚手架时,脚手架会设置此属性。

属性:

GetAccess
public
SetAccess
private

测试失败后是否保留临时文件夹及其内容,以逻辑值 0 (false) 或 1 (true) 形式返回。失败的情况包括使用脚手架的测试中发生验证、断言或致命断言失败以及未捕获的错误。默认情况下,当框架遇到故障时,它会拆解脚手架,该脚手架会删除临时文件夹及其内容。

此属性由 PreservingOnFailure 名称-值参量设置。

属性:

GetAccess
public
SetAccess
private

临时文件夹名称的后缀,以字符向量形式返回。

此属性由 WithSuffix 名称-值参量设置。

属性:

GetAccess
public
SetAccess
private

示例

全部折叠

使用 TemporaryFolderFixture 实例为测试创建临时文件夹。

在当前文件夹内一个名为 WritingToFileTest.m 的文件中创建 WritingToFileTest 类。在类中定义一个 Test 方法,该方法写入临时文件夹中的一个文件,然后验证该文件的内容。要为您的测试创建临时文件夹,请使用 TemporaryFolderFixture 实例。

classdef WritingToFileTest < matlab.unittest.TestCase
    methods (Test)
        function testWithTemporaryFolder(testCase)
            import matlab.unittest.fixtures.TemporaryFolderFixture
            fixture = testCase.applyFixture(TemporaryFolderFixture);
            
            file = fullfile(fixture.Folder,"myFile.txt");
            fid = fopen(file,"w");
            testCase.addTeardown(@fclose,fid)
            testCase.assertNotEqual(fid,-1,"IO Problem")
            txt = repmat("ab",1,1000);
            dataToWrite = join(txt);
            fprintf(fid,"%s",dataToWrite);
            testCase.verifyEqual(string(fileread(file)),dataToWrite)
        end
    end
end

运行测试。测试框架设置脚手架,该脚手架会创建一个临时文件夹。在测试后,框架会拆解脚手架,这将删除临时文件夹及其内容。在此示例中,测试通过。

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

为测试创建一个临时文件夹,该文件夹在测试失败后仍会保留。

在当前文件夹内一个名为 PersistentFolderTest.m 的文件中创建 PersistentFolderTest 类。在测试类中,使用 TemporaryFolderFixture 实例创建一个在测试失败后仍会保留的临时文件夹。为了演示需要,此示例中的测试有意设置为失败。

classdef PersistentFolderTest < matlab.unittest.TestCase
    methods (Test)
        function testWithTemporaryFolder(testCase)
            import matlab.unittest.fixtures.TemporaryFolderFixture
            testCase.applyFixture(TemporaryFolderFixture ...
                ("PreservingOnFailure",true,"WithSuffix","_TestData"));

            % Failing test
            act = 3.1416;
            exp = pi;
            testCase.verifyEqual(act,exp)
        end
    end
end

运行测试。测试失败,但临时文件夹仍会保留。测试诊断包含临时文件夹的完整路径。

runtests("PersistentFolderTest");
Running PersistentFolderTest

================================================================================
Verification failed in PersistentFolderTest/testWithTemporaryFolder.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEqual failed.
    --> The numeric values are not equal using "isequaln".
    --> Failure table:
            Actual        Expected               Error               RelativeError    
            ______    ________________    ____________________    ____________________
                                                                                      
            3.1416    3.14159265358979    7.34641020683213e-06    2.33843499679617e-06
    
    Actual Value:
       3.141600000000000
    Expected Value:
       3.141592653589793
    ----------------------
    Additional Diagnostic:
    ----------------------
    Temporary folder preserved on failure: C:\Temp\tpa00d0db0_45f0_4b3f_9ce9_3323db8df9de_TestData
    ------------------
    Stack Information:
    ------------------
    In C:\work\PersistentFolderTest.m (PersistentFolderTest.testWithTemporaryFolder) at 11
================================================================================

[Terse] Diagnostic logged (2022-10-04 09:23:49):
Because of a failure in the test using the TemporaryFolderFixture, the following folder will not be deleted:
C:\Temp\tpa00d0db0_45f0_4b3f_9ce9_3323db8df9de_TestData
.
Done PersistentFolderTest
__________

Failure Summary:

     Name                                          Failed  Incomplete  Reason(s)
    ===========================================================================================
     PersistentFolderTest/testWithTemporaryFolder    X                 Failed by verification.

提示

  • 您可以使用 createTemporaryFolder 方法为您的测试创建一个临时文件夹,而不是使用 TemporaryFolderFixture 类。但是,当您使用 TemporaryFolderFixture 类创建临时文件夹时,可以使用更多功能。

  • TemporaryFolderFixtureWorkingFolderFixture 类都会创建产生临时文件夹的脚手架。但是,使用 WorkingFolderFixture 创建的脚手架会也会将临时文件夹设置为当前文件夹。

版本历史记录

在 R2013b 中推出