Main Content

matlab.unittest.fixtures.PathFixture class

Package:matlab.unittest.fixtures

Fixture for temporarily adding folders to theMATLABpath

Description

ThePathFixtureclass 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 addsmyFolderand 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 Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and encloseNamein quotes.

Indicator to include subfolders offolders在的路径,指定为falseortrue(logical0or1). This value isfalseby default. If you specifytrue, the testing framework includes subfolders offolderson the path. Package, class, and private folders are not added to the path, even when this argument istrue.

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

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

Properties

Folders

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

IncludeSubfolders

Indicator to include subfolders offolders在的路径,指定为falseortrue(logical0or1). This property is read-only. It isfalseby default, but you can specify it astrueduring 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

句柄。学习如何处理类影响复制operations, seeCopying Objects.

Examples

collapse all

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

This example assumes thatfolderAandfolderBexist in your current folder. Create the folders if they do not exist.

if~isfolder('folderA') mkdirfolderAendif~isfolder('folderB') mkdirfolderBend

In your current folder, create thePathFixtureTestclass. The class addsfolderAandfolderBto the path using aPathFixtureinstance. Then, it asserts that the path contains the names of the folders.

classdefPathFixtureTest < matlab.unittest.TestCasemethods(Test)functiontest1(testCase) importmatlab.unittest.fixtures.PathFixtureimportmatlab.unittest.constraints.ContainsSubstringf = testCase.applyFixture(PathFixture(["folderA","folderB"])); testCase.assertThat(path,ContainsSubstring(f.Folders(1))) testCase.assertThat(path,ContainsSubstring(f.Folders(2)))endendend

Run the test. Because bothfolderAandfolderBare 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 theSharedAddPathFixtureTestclass. This example assumes that the subfolderhelperFilesexists in your current folder.

classdef(SharedTestFixtures = {...matlab.unittest.fixtures.PathFixture('helperFiles')})...SharedAddPathFixtureTest < matlab.unittest.TestCasemethods(Test)functiontest1(testCase) f = testCase.getSharedTestFixtures; disp("Added to path: "+ f.Folders)endendend

At the command prompt, run the test.

run(SharedAddPathFixtureTest);
设置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.