Understanding how testCase.addTeardown works with @path

조회 수: 10 (최근 30일)
Diaa
Diaa 2022년 7월 27일
댓글: Steven Lord 2022년 7월 27일
The following code is adopted from this blog post:
classdef GoodFixturesTest < matlab.unittest.TestCase
methods(TestMethodSetup)
function addPath1(testCase)
p = addpath(fullfile(pwd, 'path1'));
testCase.addTeardown(@path, p);
end
function addPath2(testCase)
p = addpath(fullfile(pwd, 'path2'));
testCase.addTeardown(@path, p);
end
end
methods(Test)
function runTest(~)
end
end
end
So, I need to deeply understand how addTeardown works in this line
testCase.addTeardown(@path, p);
to remove the search paths of path1 and path2 and restore the original search paths in view of
testCase.addTeardown(tearDownFcn,arg1)
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
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.
  댓글 수: 2
Diaa
Diaa 2022년 7월 27일
Many thanks for pointing this out. I would be highly grateful if you could enrich your answer by adding a complete code of how to get the desired behavior with PathFixture.
Steven Lord
Steven Lord 2022년 7월 27일
Looking at the matlab.unittest.fixtures.PathFixture class documentation page, see the "Add Folders to the Path for Testing" example for a short demonstration of how to use the fixture. Note that there's no call to addTeardown in that code. Essentially the teardown was added by applyFixture when it applied the PathFixture to the test session.
Run that test several times under different configurations and examine the state of the path (using the path function) before and after each run.
  1. Neither folderA nor folderB are on the MATLAB path before the test executes. In this case they should be on the path during the execution of the test method after the fixture has been applied, but neither will be on the MATLAB path after the test executes.
  2. Only folderA (not folderB) is on the MATLAB path before the test executes. As above they will both be on the path during the test execution after the fixture has been applied, but only folderA will be on the MATLAB path after the test finishes its execution.
  3. Both folderA and folderB are on the MATLAB path before the test executes. They will remain on the MATLAB path during test execution and will remain on the MATLAB path after the test finishes executing.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by