Fixture for changing current working folder
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.
matlab.unittest.fixtures.CurrentFolderFixture(folder) constructs a fixture for changing the current working folder to folder.
Folder to make the current working folder, specified as a string in the folder input argument.
Handle. To learn how handle classes affect copy operations, see Copying Objects in the MATLAB® documentation.
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