Main Content

matlab.unittest.fixtures.CurrentFolderFixture Class

Namespace: matlab.unittest.fixtures
Superclasses: matlab.unittest.fixtures.Fixture

Fixture for changing current folder

Description

The 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.

The matlab.unittest.fixtures.CurrentFolderFixture class is a handle class.

Creation

Description

fixture = matlab.unittest.fixtures.CurrentFolderFixture(folder) creates a fixture for changing the current folder to the specified folder and sets the Folder property.

example

Properties

expand all

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.

Attributes:

GetAccess
public
SetAccess
immutable

Examples

collapse all

Change the current folder for testing by using a CurrentFolderFixture instance.

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, create the 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 "helperFiles".

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 "helperFiles".

pwd
ans =

    'C:\work'

Run the test class. The fixture changes the current folder to helperFiles. Therefore, the test passes.

runtests("CurrentFolderTest");
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.

pwd
ans =

    'C:\work'

Version History

Introduced in R2013b