MATLAB® 検索パスからフォルダーを削除するフィクスチャを作成し、フィクスチャが無効になっている場合は環境の状態をリセットするようにテスト フレームワークに指示します。次に、複数のクラスでテストを実行する場合は、フィクスチャを共有テスト フィクスチャとして使用します。
この例では、現在のフォルダー内のサブフォルダー helperFiles
がパス上に存在することを前提としています。存在しない場合はサブフォルダーを作成し、パス上にあることを確認します。
現在のフォルダーのファイル内に、パスからフォルダーを削除することにより環境の状態を設定する RemoveFolderFromPathFixture
というフィクスチャを作成します。フィクスチャを共有テスト フィクスチャとして使用するすべてのテスト クラスを同じ環境の状態にするには、needsReset
メソッドをオーバーライドします。テスト ランナーが後続のクラスに切り替えたときに指定されたフォルダーがパス上にある場合、メソッドは true
を返します。
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
現在のフォルダーで、共有テスト フィクスチャとして RemoveFolderFromPathFixture
を使用する 3 つのテスト クラスを作成します。
SampleTestA.m
という名前のファイルに、SampleTestA
クラスを作成します。
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
SampleTestB.m
という名前のファイルに、SampleTestB
クラスを作成します。クラス内のテストは helperFiles
をパスに追加します。
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
SampleTestC.m
という名前のファイルに、SampleTestC
クラスを作成します。
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
テスト スイートを作成してテストを実行します。共有テスト フィクスチャを検証するために、テスト フレームワークはテスト ランナーが SampleTestB
および SampleTestC
に切り替えたときに needsReset
メソッドを呼び出します。
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
は RemoveFolderFromPathFixture
で設定された環境の状態を破損しません。このため、テストフレームは確立されたフィクスチャを使用して SampleTestB
を実行します。ただし、SampleTestB
は helperFiles
をパスに追加することにより、環境の状態を破損します。フレームワークはフィクスチャを破棄し、SampleTestB
と SampleTestC
への呼び出しの間にフィクスチャをセットアップします。