Main Content

matlab.unittest.fixtures.PathFixture Class

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

Fixture for adding folders to the MATLAB path

Description

The matlab.unittest.fixtures.PathFixture class provides a fixture for adding folders to the MATLAB® path. When the testing framework sets up the fixture, the fixture adds the specified folders to the path. When the framework tears down the fixture, the fixture restores the path to its original state.

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

Creation

Description

fixture = matlab.unittest.fixtures.PathFixture(folders) creates a fixture for adding the specified folders to the path.

example

fixture = matlab.unittest.fixtures.PathFixture(folders,Name,Value) sets additional options using one or more name-value arguments. For example, fixture = matlab.unittest.fixtures.PathFixture("myFolder","IncludingSubfolders",true) creates a fixture that adds myFolder and all of its subfolders to the path.

Input Arguments

expand all

Folders to add to the path, specified as a string array, character vector, or cell array of character vectors. You can specify a folder in folders with a relative path, but the relative path must be in the current folder. Otherwise, you must use a full path to specify the folder. If any of the specified folders does not exist, MATLAB throws an error.

This argument sets the Folders property.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: fixture = matlab.unittest.fixtures.PathFixture("myFolder",IncludingSubfolders=true)

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: fixture = matlab.unittest.fixtures.PathFixture("myFolder","IncludingSubfolders",true)

Whether to include the subfolders of folders on the path, specified as a numeric or logical 0 (false) or 1 (true). By default, the fixture does not include the subfolders of folders on the path.

Class, namespace, private, and resources subfolders cannot be included on the path, even when the value is true.

This argument sets the IncludeSubfolders property.

Where on the path to add the folders, specified as "begin" or "end". By default, the fixture adds folders to the beginning (top) of the path. If you specify the value as "end", the fixture adds folders to the end (bottom) of the path.

If you specify this argument along with IncludingSubfolders, the fixture adds the specified folders and their subfolders to the top or bottom of the path as a single block.

This argument sets the Position property.

Properties

expand all

Full paths to the folders to add to the path, returned as a string array.

This property is set by the folders input argument.

Attributes:

GetAccess
public
SetAccess
private

Whether to include subfolders on the path, returned as a logical 0 (false) or 1 (true). By default, the fixture does not include subfolders on the path.

This property is set by the IncludingSubfolders name-value argument.

Attributes:

GetAccess
public
SetAccess
immutable

Where on the path to add the folders, returned as 'begin' or 'end'. By default, the fixture adds the folders to the top of the path.

This property is set by the Position name-value argument.

Attributes:

GetAccess
public
SetAccess
immutable

Examples

collapse all

Temporarily add folders to the path for testing by using a PathFixture instance.

This example assumes that folderA and folderB exist in your current folder. Create the folders if they do not exist.

[~,~] = mkdir("folderA")
[~,~] = mkdir("folderB")

In a file named PathFixtureTest.m in your current folder, create the PathFixtureTest class. The test uses a PathFixture instance to add folderA and folderB to the path. Then, it verifies that the path contains the names of the folders.

classdef PathFixtureTest < matlab.unittest.TestCase
    methods (Test)
        function testPath(testCase)
            import matlab.unittest.fixtures.PathFixture
            import matlab.unittest.constraints.ContainsSubstring
            f = testCase.applyFixture(PathFixture(["folderA" "folderB"]));
            testCase.verifyThat(path,ContainsSubstring(f.Folders(1)))
            testCase.verifyThat(path,ContainsSubstring(f.Folders(2)))
        end
    end
end

Run the test. Because both folderA and folderB are on the path, the test passes. After testing, the framework tears down the fixture, which restores the path to its previous state.

runtests("PathFixtureTest");
Running PathFixtureTest
.
Done PathFixtureTest
__________

Version History

Introduced in R2013b