matlab.unittest.fixtures.TemporaryFolderFixture class

Package: matlab.unittest.fixtures

Fixture for creating a temporary folder

Description

The matlab.unittest.fixtures.TemporaryFolderFixture provides a fixture to create a temporary folder. When the testing framework sets up the fixture, it creates the temporary folder. When it tears down the fixture, it deletes the folder and all its contents. Before it deletes the folder, the fixture clears from memory the definitions of any MATLAB-files, P-files, and MEX-files that are defined in the temporary folder.

Both the TemporaryFolderFixture and WorkingFolderFixture fixtures create a temporary folder. Unlike the WorkingFolderFixture, the TemporaryFolderFixture does not set the folder as the current working folder.

Construction

matlab.unittest.fixtures.TemporaryFolderFixture constructs a fixture for creating a temporary folder.

matlab.unittest.fixtures.TemporaryFolderFixture(Name,Value) constructs a fixture for creating a temporary folder with additional options specified by one or more Name,Value pair arguments.

Input Arguments

expand all

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Indicator of whether the temporary folder and its contents are preserved in the event of a test failure, specified as false or true (logical 0 or 1). This property is false by default. You can specify it as true during fixture construction.

Data Types: logical

Suffix for temporary folder name, specified as a character vector.

Properties

Folder

Absolute path of the folder created by the fixture, specified as a character vector.

PreserveOnFailure

Indicator of whether the temporary folder and its contents are preserved in the event of a test failure. This property is logical(0) or logical(1). It is logical(0) by default but is set to logical(1) if the 'PreservingOnFailure' input value is set to true during fixture construction.

Suffix

Suffix used for temporary folder, specified as a character vector in the Name,Value pair argument, 'WithSuffix'.

Copy Semantics

Handle. To learn how handle classes affect copy operations, see Copying Objects.

Examples

collapse all

Create the following tempFolderFixtureTest class definition on your MATLAB® path.

classdef tempFolderFixtureTest < matlab.unittest.TestCase
    methods(Test)
        function test1(testCase)
            import matlab.unittest.fixtures.TemporaryFolderFixture
            
            tempFixture = testCase.applyFixture(TemporaryFolderFixture);
            
            disp(['The temporary folder: ' tempFixture.Folder])
        end
    end
end

At the command prompt, run the test.

run(tempFolderFixtureTest);
Running tempFolderFixtureTest
The temporary folder: C:\Temp\tpfb1ae2cf_c9de_4de3_9557_00d52bfcc1b2
.
Done tempFolderFixtureTest
__________

The name of the temporary folder varies.

Create the following anotherTempFolderFixtureTest class definition on your MATLAB path. For the purposes of this example, the test1 function contains an assertion that causes test failure.

classdef anotherTempFolderFixtureTest < matlab.unittest.TestCase
    methods(Test)
        function test1(testCase)
            import matlab.unittest.fixtures.TemporaryFolderFixture
            
            testCase.applyFixture(TemporaryFolderFixture( ...
                'PreservingOnFailure',true,'WithSuffix','TestData'));
            
            % Failed assertion, preserved temporary folder
            testCase.assertEqual(1,2)
        end
    end
end

At the command prompt, run the test.

run(anotherTempFolderFixtureTest);
Running anotherTempFolderFixtureTest

================================================================================
Assertion failed in anotherTempFolderFixtureTest/test1 and it did not run to completion.

    ---------------------
    Framework Diagnostic:
    ---------------------
    assertEqual failed.
    --> The values are not equal using "isequaln".
    --> Failure table:
                Actual    Expected    Error    RelativeError
                ______    ________    _____    _____________
            
                1         2           -1       -0.5         
    
    Actual double:
             1
    Expected double:
             2

    ------------------
    Stack Information:
    ------------------
    In C:\Documents\anotherTempFolderFixtureTest.m (anotherTempFolderFixtureTest.test1) at 10
================================================================================
   [Terse] Diagnostic logged (2014-04-01T13:50:51): 
Because of a failure in the test using the TemporaryFolderFixture, the following folder will not be deleted:
C:\Temp\tp9f5aa9f1_ead1_4462_91f2_08bbe7d0316cTestData


.
Done anotherTempFolderFixtureTest
__________

Failure Summary:

     Name                                Failed  Incomplete  Reason(s)
    ==============================================================================
     anotherTempFolderFixtureTest/test1    X         X       Failed by assertion.

The test failed and the temporary folder persists. You can open the temporary folder, shown here as C:\Temp\tp9f5aa9f1_ead1_4462_91f2_08bbe7d0316cTestData, and examine any contents.

Introduced in R2013b