matlab.unittest.fixtures.CurrentFolderFixture class

Package: matlab.unittest.fixtures

Fixture for changing current working folder

Description

The CurrentFolderFixture class provides a fixture for changing the current working folder. When the test framework sets up the fixture, it changes the working folder. When the test framework tears down the fixture, it restores the working folder to its previous state.

Construction

matlab.unittest.fixtures.CurrentFolderFixture(folder) constructs a fixture for changing the current working folder to folder.

Input Arguments

expand all

Folder to make the current working folder, specified as a character vector. MATLAB® throws an error if folder does not exist.

Properties

Folder

Folder to make the current working folder, specified as a character vector in the folder input argument.

Copy Semantics

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

Examples

collapse all

Create the following changeFolderFixtureTest class definition on your MATLAB path. This example assumes that the subfolder helperFiles exists in your working folder. Create the changeToFolderin your working folder if it does not exist.

The test1 function includes a call to pwd to demonstrate the current path changed to the helperFiles folder.

classdef changeFolderFixtureTest < matlab.unittest.TestCase
    methods(Test)
        function test1(testCase)
            import matlab.unittest.fixtures.CurrentFolderFixture
            
            changeToFolder = 'helperFiles';
            testCase.applyFixture(CurrentFolderFixture ...
                (changeToFolder));
            pwd
        end
    end
end

At the command prompt, run the test. For the purposes of this example, call pwd before and after run to show the fixture was properly torn down and the path returned to the pre-test state.

currentFolderBeforeTest = pwd
run(changeFolderFixtureTest);
currentFolderAfterTest = pwd
currentFolderBeforeTest =

H:\Documents\doc_examples

Running changeFolderFixtureTest

ans =

H:\Documents\doc_examples\helperFiles

.
Done changeFolderFixtureTest
__________


currentFolderAfterTest =

H:\Documents\doc_examples