Understanding how testCase.addTeardown works with @path
조회 수: 4(최근 30일)
The following code is adopted from this blog post:
classdef GoodFixturesTest < matlab.unittest.TestCase
p = addpath(fullfile(pwd, 'path1'));
p = addpath(fullfile(pwd, 'path2'));
So, I need to deeply understand how addTeardown works in this line
to remove the search paths of path1 and path2 and restore the original search paths in view of
and the path function syntax path(newpath).
In other words, what is the meaning of passing all the search paths (including path1 and path2) stored in p to the function handle @path in order to restore the original search paths without the full paths of the newly added subfolders path1 and path2?
Steven Lord 2022년 7월 27일
From the addpath documentation: "oldpath = addpath(___) additionally returns the path prior to adding the specified folders." So p does not include the path1 or path2 subdirectories (unless they were already on the path.)
This is a common pattern in MATLAB. Many functions that change some of the state of MATLAB return the state prior to the change to facilitate "undoing" the state change.
Of course, this example is a bit artificial to me. For this I wouldn't use addTeardown manually to restore the previous state. I'd create and apply a matlab.unittest.fixtures.PathFixture. The PathFixture will automatically restore the previous state of the path when it gets torn down.