Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

getSharedTestFixtures

クラス: matlab.unittest.TestCase
名前空間: matlab.unittest

共有テスト フィクスチャへのアクセスの提供

説明

fixtures = getSharedTestFixtures(testCase) は、テスト ケースの共有テスト フィクスチャへのアクセスを提供し、それらを matlab.unittest.fixtures.Fixture オブジェクトの配列として返します。共有テスト フィクスチャは、TestCase サブクラスの SharedTestFixtures 属性を使用して指定されます。

fixtures = getSharedTestFixtures(testCase,fixtureClassName) は、クラスの名前が fixtureClassName である共有テスト フィクスチャのみを返します。

入力引数

すべて展開する

テスト ケース。matlab.unittest.TestCase オブジェクトとして指定します。

共有テスト フィクスチャを定義するクラスの完全修飾名。string スカラーまたは文字ベクトルとして指定します。

例: "matlab.unittest.fixtures.PathFixture"

属性

Sealedtrue

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

すべて展開する

getSharedTestFixtures メソッドを使用してテストの共有フィクスチャにアクセスします。

この例では、サブフォルダー helperFiles が現在のフォルダー内に存在していると仮定します。サブフォルダーが存在しない場合は作成します。

[~,~] = mkdir("helperFiles")

現在のフォルダー内のファイルで、2 つの共有テスト フィクスチャを使用する SampleTest テスト クラスを作成します。この例では、説明のために、Test メソッドでフィクスチャにアクセスしてそれらの検定を実行します。

classdef (SharedTestFixtures={ ...
        matlab.unittest.fixtures.PathFixture("helperFiles"), ...
        matlab.unittest.fixtures.TemporaryFolderFixture}) ...
        SampleTest < matlab.unittest.TestCase
    methods (Test)
        function testFixtureCount(testCase)
            % Test the number of shared test fixtures
            f = testCase.getSharedTestFixtures;
            testCase.verifyNumElements(f,2)
        end

        function testPath(testCase)
            % Test the search path
            import matlab.unittest.constraints.ContainsSubstring
            f = testCase.getSharedTestFixtures( ...
                "matlab.unittest.fixtures.PathFixture");
            testCase.verifyThat(path,ContainsSubstring(f.Folder))
        end

        function testTempFolder(testCase)
            % Test writing to the temporary folder
            import matlab.unittest.constraints.IsFile
            f = testCase.getSharedTestFixtures( ...
                "matlab.unittest.fixtures.TemporaryFolderFixture");
            tempFolderName = f.Folder;
            filename = string(tempFolderName) + filesep + "myFile.dat";
            writematrix(magic(20),filename)
            testCase.verifyThat(filename,IsFile)
        end
    end
end

テスト クラスを実行します。テスト フレームワークによって、最初にフィクスチャがセットアップされ、テストが実行されてから、フィクスチャが破棄されます。この例では、すべてのテストがパスします。

results = runtests("SampleTest");
Setting up PathFixture
Done setting up PathFixture: Added 'C:\work\helperFiles' to the path.
__________

Setting up TemporaryFolderFixture
Done setting up TemporaryFolderFixture: Created the temporary folder "C:\Temp\tpf8cf6d27_3c19_42ce_9c0f_d3a130a077f0".
__________

Running SampleTest
...
Done SampleTest
__________

Tearing down TemporaryFolderFixture
Done tearing down TemporaryFolderFixture: Deleted the temporary folder "C:\Temp\tpf8cf6d27_3c19_42ce_9c0f_d3a130a077f0" and all its contents.
__________

Tearing down PathFixture
Done tearing down PathFixture: Restored the path to its original state.
__________

バージョン履歴

R2013b で導入