Main Content

needsReset

クラス: matlab.unittest.fixtures.Fixture
名前空間: matlab.unittest.fixtures

共有テスト フィクスチャをリセットする必要があるかどうかの判別

R2020b 以降

説明

tf = needsReset(fixture) は共有テスト fixture の有効性をテスト フレームワークにレポートします。fixture が無効であり、リセットする必要がある場合、メソッドは logical 1 (true) を返します。その他の場合は logical 0 (false) を返します。共有テスト フィクスチャは、フィクスチャで構成されたテスト環境の状態がテスト セッション全体にわたり維持される場合は有効です。

共有テスト フィクスチャを使用するテスト クラスの場合、テスト ランナーが後続のクラスに切り替わるたびに、フレームワークは needsReset メソッドを呼び出します。メソッドが true を返す場合、フレームワークは自動的に共有テスト フィクスチャを破棄し、後続のクラス用にフィクスチャをセットアップします。フレームワークは、teardown メソッドまたは addTeardown メソッドで定義されたアクションを実行して無効になったフィクスチャを破棄し、setup メソッドで定義されたアクションを実行してフィクスチャをセットアップします。したがって、needsReset の実装にフィクスチャのセットアップ アクションまたは破棄アクションを実行するコードを含めることはできません。

入力引数

すべて展開する

検証する共有テスト フィクスチャ。matlab.unittest.fixtures.Fixture クラスのインスタンスとして指定します。

属性

Accessprotected

メソッドの属性の詳細については、メソッドの属性を参照してください。

すべて展開する

MATLAB® 検索パスからフォルダーを削除するフィクスチャを作成し、フィクスチャが無効になっている場合は環境の状態をリセットするようにテスト フレームワークに指示します。次に、複数のクラスでテストを実行する場合は、フィクスチャを共有テスト フィクスチャとして使用します。

この例では、現在のフォルダー内のサブフォルダー helperFiles がパス上に存在することを前提としています。存在しない場合はサブフォルダーを作成し、パス上にあることを確認します。

if ~isfolder('helperFiles')
    mkdir helperFiles
end
addpath('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 メソッドを呼び出します。

suite = [testsuite('SampleTestA') testsuite('SampleTestB') ...
    testsuite('SampleTestC')];
runner = matlab.unittest.TestRunner.withTextOutput;   
results = runner.run(suite);
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
__________

SampleTestARemoveFolderFromPathFixture で設定された環境の状態を破損しません。このため、テストフレームは確立されたフィクスチャを使用して SampleTestB を実行します。ただし、SampleTestBhelperFiles をパスに追加することにより、環境の状態を破損します。フレームワークはフィクスチャを破棄し、SampleTestBSampleTestC への呼び出しの間にフィクスチャをセットアップします。

バージョン履歴

R2020b で導入