Fixture for changing current folder
matlab.unittest.fixtures.CurrentFolderFixture class provides a fixture
for changing the current folder. When the testing framework sets up the fixture, the fixture
changes the current folder to a specified folder. When the framework tears down the fixture,
the fixture changes the current folder back to the original folder.
matlab.unittest.fixtures.CurrentFolderFixture class is a
Folder — Full path to target folder
Full path to the target folder, returned as a character vector. Specify the value of this property during creation of the fixture as a string scalar or character vector. You can specify a relative path, but the relative path must be in the current folder. Otherwise, you must specify a full path.
MATLAB® throws an error if
folder does not exist.
Change Current Folder While Testing
Change the current folder for testing by using a
This example assumes that the folder
helperFiles exists in your
current folder. Create the folder if it does not exist.
[~,~] = mkdir("helperFiles")
In a file named
CurrentFolderTest.m in your current folder,
CurrentFolderTest class that uses a fixture to change the
current folder to
helperFiles. To simplify this example, the test
verifies that the full path to the new current folder contains the substring
classdef CurrentFolderTest < matlab.unittest.TestCase methods (Test) function testCurrentFolder(testCase) import matlab.unittest.fixtures.CurrentFolderFixture testCase.applyFixture(CurrentFolderFixture("helperFiles")) testCase.verifySubstring(pwd,"helperFiles") end end end
Before running the
CurrentFolderTest class, first return the path to
the current folder. The returned path does not contain
ans = 'C:\work'
Run the test class. The fixture changes the current folder to
helperFiles. Therefore, the test passes.
Running CurrentFolderTest . Done CurrentFolderTest __________
Once the test run is complete, the testing framework tears down the fixture and the environment returns to its original state.
ans = 'C:\work'
Introduced in R2013b