Main Content

matlab.unittest.fixtures.PathFixture class

Package: matlab.unittest.fixtures

Fixture for temporarily adding folders to the MATLAB path

Description

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

Construction

matlab.unittest.fixtures.PathFixture(folders) constructs a fixture for temporarily adding folders to the MATLAB path.

matlab.unittest.fixtures.PathFixture(folders,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 all of its subfolders to the path.

Input Arguments

expand all

Folders to add to the MATLAB path, specified as a string array, character vector, or cell array of character vectors. If any of the specified folders 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.

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

Where to add the folders, specified as 'begin' or 'end'. The default value is 'begin', which adds folders to the beginning (top) of the path.

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

Properties

Folders

Folders to add to the MATLAB path, specified as a string array. This property is read-only and corresponds to the folders input argument.

IncludeSubfolders

Indicator to include subfolders of folders 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

Where to add folders, 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

Temporarily add two folders to the MATLAB search path using a fixture.

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

if ~isfolder('folderA')
    mkdir folderA
end
if ~isfolder('folderB')
    mkdir folderB
end

In your current folder, create the PathFixtureTest class. The class adds folderA and folderB to the path using a PathFixture instance. Then, it asserts that the path contains the names of the folders.

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

Run the test. Because both folderA and folderB are on the path, the test passes.

runtests('PathFixtureTest')
Running PathFixtureTest
.
Done PathFixtureTest
__________
ans = 
  TestResult with properties:

          Name: 'PathFixtureTest/test1'
        Passed: 1
        Failed: 0
    Incomplete: 0
      Duration: 0.4069
       Details: [1×1 struct]

Totals:
   1 Passed, 0 Failed, 0 Incomplete.
   0.40688 seconds testing time.

After the test runs, the framework restores the path to its previous state.

In your current folder, create the SharedAddPathFixtureTest class. This example assumes that the subfolder helperFiles exists in your current 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.Folders)
        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 test runs, the framework restores the path to its previous state.