Main Content

matlab.unittest.TestCase.forInteractiveUse

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

対話型で使用するテスト ケースを作成

説明

testCase = matlab.unittest.TestCase.forInteractiveUse は、対話型テスト用に構成されたテスト ケースを作成します。返される TestCase インスタンスはコマンド プロンプトでの実験に適しています。パスと失敗の両方のイベントについて、画面にメッセージを出力して検定に反応します。

testCase = matlab.unittest.TestCase.forInteractiveUse(testClass) は、指定されたテスト クラスの対話型テスト用のインスタンスを作成します。

testCase = matlab.unittest.TestCase.forInteractiveUse(testClass,ApplySharedTestFixtures=tf) は、対話型テスト用のテスト クラスに関連付けられている共有テスト フィクスチャをすべて適用するかどうかも指定します。tftrue の場合、メソッドは testClass から対話型のテスト ケースを作成するときに共有テスト フィクスチャを設定します。これらのフィクスチャは、テスト ケースがスコープ外になるとテスト フレームワークで自動的に破棄されます。共有テスト フィクスチャは、TestCase サブクラスの SharedTestFixtures 属性を使用して指定されます。 (R2024a 以降)

入力引数

すべて展開する

matlab.unittest.TestCase から派生するテスト クラス。matlab.metadata.Class インスタンスとして指定します。

例: ?ExampleTest

R2024a 以降

testClass に関連付けられている共有テスト フィクスチャを適用するオプション。数値または logical 0 (false) または 1 (true) として指定します。既定では、テスト クラスから対話型のテスト ケースを作成するときにメソッドは共有テスト フィクスチャを無視します。

属性

Statictrue

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

すべて展開する

実際の値が指定された部分文字列を含むかテストします。

対話型テスト用にテスト ケースを作成します。

testCase = matlab.unittest.TestCase.forInteractiveUse;

実際の値を定義します。

actual = "This is a long message.";

actual にテキスト "long" が含まれていることを検証します。

verifySubstring(testCase,actual,"long")
Verification passed.

大文字と小文字の区別が重要であることを示します。このテストは、actual"Long" が含まれていないため失敗します。

verifySubstring(testCase,actual,"Long","Test is case sensitive.")
Verification failed.
    ----------------
    Test Diagnostic:
    ----------------
    Test is case sensitive.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifySubstring failed.
    --> The value does not contain the substring.
    
    Actual Value:
        "This is a long message."
    Expected Substring:
        "Long"
    ------------------
    Stack Information:
    ------------------
    In C:\work\TestForSubstringsExample.m (TestForSubstringsExample) at 22

部分文字列が実際の文字列より長い場合にテストが失敗することを示します。

verifySubstring(testCase,actual,"This is a long message with extra words.")
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifySubstring failed.
    --> The value does not contain the substring.
    
    Actual Value:
        "This is a long message."
    Expected Substring:
        "This is a long message with extra words."
    ------------------
    Stack Information:
    ------------------
    In C:\work\TestForSubstringsExample.m (TestForSubstringsExample) at 27

テスト クラスの Test メソッドを対話形式で実行します。

現在のフォルダー内の ZerosTest.m という名前のファイルに、関数 zeros をテストする ZerosTest クラスを作成します。

classdef ZerosTest < matlab.unittest.TestCase
    properties (TestParameter)
        type = {'single','double','uint16'};
        size = struct("s2d",[3 3],"s3d",[2 5 4]);
    end
    
    methods (Test)
        function testClass(testCase,size,type)
            testCase.verifyClass(zeros(size,type),type)
        end
        
        function testSize(testCase,size)
            testCase.verifySize(zeros(size),size)
        end
        
        function testDefaultClass(testCase)
            testCase.verifyClass(zeros,"double")
        end
        
        function testDefaultSize(testCase)
            testCase.verifySize(zeros,[1 1])
        end
        
        function testDefaultValue(testCase)
            testCase.verifyEqual(zeros,0)
        end
    end
end

対話型テスト用に ZerosTest クラスのインスタンスを作成します。

testCase = matlab.unittest.TestCase.forInteractiveUse(?ZerosTest);

テスト ケースを使用して testSize メソッドを対話形式で呼び出します。テストはパスします。

testCase.testSize([5 10])
Verification passed.

R2024a 以降

テスト クラスの Test メソッドを対話形式で実行するときに共有テスト フィクスチャを使用します。

この例では、現在のフォルダーに 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

対話型テスト用に SampleTest クラスのインスタンスを作成します。SampleTest のテストは共有テスト フィクスチャに依存するため、テスト ケースを作成するときにフィクスチャを適用します。

testCase = matlab.unittest.TestCase.forInteractiveUse(?SampleTest, ...
    ApplySharedTestFixtures=true);

テスト ケースを使用して testFixtureCount メソッドを対話形式で呼び出します。共有テスト フィクスチャをテストで使用できるため、テストはパスします。フィクスチャを適用せずにテスト ケースを作成すると、テストは失敗します。

testCase.testFixtureCount
Verification passed.

バージョン履歴

R2014a で導入

すべて展開する