Main Content

matlab.unittest.fixtures.CurrentFolderFixture Class

R2026b

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.

example

Input Arguments

expand all

Path to the target folder, specified as a string scalar or character vector. The value can be a path relative to the current folder or an absolute path. MATLAB® throws an error if folder does not exist.

This argument sets the Folder property.

Properties

expand all

In addition to these properties, the CurrentFolderFixture class inherits properties from the Fixture class.

Absolute path to the target folder, specified as a string scalar or character vector, and stored as a character vector.

You should set this property during fixture creation by using the folder input argument.

Attributes:

GetAccess
public
SetAccess
immutable

Absolute path to the original working folder, returned as a character vector. The fixture sets this property when the framework sets up the fixture.

Attributes:

GetAccess
public
SetAccess
private

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

result = 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