Create a fixture that removes a folder from the MATLAB® search path, and instruct the testing framework to reset the environment state if the fixture is invalidated. Then, use the fixture as a shared test fixture when running tests in multiple classes.
This example assumes that the subfolder helperFiles
within your current folder exists on the path. Create the subfolder if it does not exist and ensure that it is on the path.
In a file in your current folder, create a fixture named RemoveFolderFromPathFixture
that sets the environment state by removing a folder from the path. To ensure the same environment state for all of the test classes that use the fixture as a shared test fixture, override the needsReset
method. The method returns true
if the specified folder is on the path when the test runner switches to the subsequent class.
classdef RemoveFolderFromPathFixture < matlab.unittest.fixtures.Fixture
properties (SetAccess = immutable)
Folder (1,1) string % Full path to the folder
end
methods
function fixture = RemoveFolderFromPathFixture(folder)
fixture.Folder = folder;
end
function setup(fixture)
originalPath = path;
fixture.addTeardown(@()path(originalPath));
rmpath(fixture.Folder)
end
end
methods (Access = protected)
function tf = isCompatible(fixture1,fixture2)
tf = fixture1.Folder == fixture2.Folder;
end
function tf = needsReset(fixture)
foldersOnPath = split(path,pathsep);
tf = ismember(fixture.Folder,foldersOnPath);
end
end
end
In your current folder, create three test classes that use RemoveFolderFromPathFixture
as a shared test fixture.
In a file named SampleTestA.m
, create the SampleTestA
class.
classdef (SharedTestFixtures = { ...
RemoveFolderFromPathFixture(fullfile(pwd,'helperFiles'))}) ...
SampleTestA < matlab.unittest.TestCase
methods (Test)
function test1(testCase)
import matlab.unittest.constraints.ContainsSubstring
f = testCase.getSharedTestFixtures;
testCase.assertThat(path,~ContainsSubstring(f.Folder))
end
end
end
In a file named SampleTestB.m
, create the SampleTestB
class. The test in the class adds helperFiles
to the path.
classdef (SharedTestFixtures = { ...
RemoveFolderFromPathFixture(fullfile(pwd,'helperFiles'))}) ...
SampleTestB < matlab.unittest.TestCase
methods (Test)
function test1(testCase)
import matlab.unittest.constraints.ContainsSubstring
f = testCase.getSharedTestFixtures;
addpath('helperFiles')
testCase.assertThat(path,ContainsSubstring(f.Folder))
end
end
end
In a file named SampleTestC.m
, create the SampleTestC
class.
classdef (SharedTestFixtures = { ...
RemoveFolderFromPathFixture(fullfile(pwd,'helperFiles'))}) ...
SampleTestC < matlab.unittest.TestCase
methods (Test)
function test1(testCase)
import matlab.unittest.constraints.ContainsSubstring
f = testCase.getSharedTestFixtures;
testCase.assertThat(path,~ContainsSubstring(f.Folder))
end
end
end
Create a test suite and run the tests. To validate the shared test fixture, the testing framework calls the needsReset
method when the test runner switches to SampleTestB
and SampleTestC
.
Setting up RemoveFolderFromPathFixture
Done setting up RemoveFolderFromPathFixture
__________
Running SampleTestA
.
Done SampleTestA
__________
Running SampleTestB
.
Done SampleTestB
__________
Tearing down RemoveFolderFromPathFixture
Done tearing down RemoveFolderFromPathFixture
__________
Setting up RemoveFolderFromPathFixture
Done setting up RemoveFolderFromPathFixture
__________
Running SampleTestC
.
Done SampleTestC
__________
Tearing down RemoveFolderFromPathFixture
Done tearing down RemoveFolderFromPathFixture
__________
SampleTestA
does not corrupt the environment state set by RemoveFolderFromPathFixture
. Therefore, the framework uses the established fixture for running SampleTestB
. However, SampleTestB
corrupts the environment state by adding helperFiles
to the path. The framework tears down the fixture and sets it up between calls to SampleTestB
and SampleTestC
.