matlab.unittest.fixtures.PathFixture class

Package: matlab.unittest.fixtures

Fixture for adding a folder to the MATLAB path

Description

The PathFixture class provides a fixture for adding a folder to the MATLAB® path. When the test framework sets up the fixture, it adds the specified folder to the path. When the test framework tears down the fixture, it restores the MATLAB path to its previous state.

Construction

matlab.unittest.fixtures.PathFixture(folder) constructs a fixture for adding a folder to the MATLAB path. When the test framework sets up the fixture, it adds folder to the path. When it tears down the fixture, it restores the MATLAB path to its previous state.

matlab.unittest.fixtures.PathFixture(folder,Name,Value) constructs a fixture with additional options specified by one or more Name,Value pair arguments. For example, matlab.unittest.fixtures.PathFixture('myFolder','IncludingSubfolders',true) constructs a fixture that adds myFolder and any of its subfolders to the path.

Input Arguments

expand all

Folder to add to the MATLAB path, specified as a character vector. If folder does not exist, MATLAB throws an error.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Setting to include subfolders of folder on the path, specified as false or true (logical 0 or 1). This value is false by default. If you specify it as true, the test framework includes subfolders of folder on the path. Package, class, and private folders are not added to the path, even when this property is true.

Location of where on the path to add the folder, specified as 'begin' or 'end'. The default value is 'begin', which adds folder to the beginning (top) of the path.

If you use this option with IncludingSubfolders, the fixture adds the folder and its subfolders to the top or bottom of the path as a single block with folder on the top.

Properties

Folder

Folder to add to the MATLAB path, specified as a character vector in the folder input argument.

IncludeSubfolders

Indicator to include subfolders of folder on the path, specified as false or true (logical 0 or 1). This property is read only. It is false by default, but you can specify it as true during construction.

Position

Indicator of where on the path to add folder, specified as 'begin' or 'end'. This property is read only. It is 'begin' by default, but you can specify it as 'end' during construction.

Copy Semantics

Handle. To learn how handle classes affect copy operations, see Copying Objects.

Examples

collapse all

Create the following addPathFixtureTest class definition on your MATLAB path. This example assumes that the subfolder,helperFiles, exists in your working folder. If it does not, define addFolder to be a folder that exists within your current folder.

classdef addPathFixtureTest < matlab.unittest.TestCase
    methods(Test)
        function test1(testCase)
            import matlab.unittest.fixtures.PathFixture
            
            addFolder = 'helperFiles';
            f = testCase.applyFixture(PathFixture(addFolder));
            disp(['Added to path: ' f.Folder])
        end
    end
end

At the command prompt, run the test.

run(addPathFixtureTest);
Running addPathFixtureTest
Added to path: H:\Documents\doc_examples\helperFiles
.
Done addPathFixtureTest
__________

After the tests finish running, the framework removes the folder from the path.

Create the following sharedAddPathFixtureTest class definition on your MATLAB path. This example assumes that the subfolder, helperFiles, exists in your working folder.

classdef (SharedTestFixtures={ ...
        matlab.unittest.fixtures.PathFixture('helperFiles')}) ...
        sharedAddPathFixtureTest < matlab.unittest.TestCase
    methods(Test)
        function test1(testCase)
            f = testCase.getSharedTestFixtures;
            disp(['Added to path: ' f.Folder])
        end
    end
end

At the command prompt, run the test.

run(sharedAddPathFixtureTest);
Setting up PathFixture
Done setting up PathFixture: Added 'H:\Documents\doc_examples\helperFiles' to the path.
__________

Running sharedAddPathFixtureTest
Added to path: H:\Documents\doc_examples\helperFiles
.
Done sharedAddPathFixtureTest
__________

Tearing down PathFixture
Done tearing down PathFixture: Restored the path to its original state.
__________

After the tests finish running, the framework removes the folder from the path.