Main Content

matlab.unittest.TestCase クラス

名前空間: matlab.unittest
スーパークラス: matlab.unittest.qualifications.Assertable, matlab.unittest.qualifications.Assumable, matlab.unittest.qualifications.FatalAssertable, matlab.unittest.qualifications.Verifiable

すべてのテスト クラスのスーパークラス

説明

matlab.unittest.TestCase クラスは、MATLAB® のすべてのテスト クラスのスーパークラスです。テスト フィクスチャのセットアップ ルーチンと破棄ルーチンを含む、テスト内容を記述および識別するためのインターフェイスを提供します。

クラスベースのテストを作成するには、TestCase クラスをサブクラス化する必要があります。サブクラスでのテストおよびテスト フィクスチャの指定には、フレームワーク固有の属性を利用できます。詳細については、TestCase のクラスの属性TestCase のメソッドの属性、およびTestCase のプロパティの属性を参照してください。

matlab.unittest.TestCase クラスは handle クラスです。

クラスの属性

抽象型
true

クラス属性の詳細については、クラスの属性を参照してください。

作成

ほとんどの場合、TestCase クラスのインスタンスを直接作成する必要はありません。テストの実行時に、テスト ランナーで TestCase インスタンスが自動的に作成されます。

コマンド ラインでの対話型のテスト用に TestCase インスタンスを作成するには、静的メソッド forInteractiveUse を使用します。

メソッド

すべて展開する

イベント

これらのイベントに加え、matlab.unittest.TestCase クラスは AssertableAssumableFatalAssertable、および Verifiable の各クラスからイベントを継承します。

イベント名トリガーイベント データイベントの属性
ExceptionThrownテスト ランナーがテスト内容で例外をキャッチしたときにトリガーされます。ExceptionEventData オブジェクトがリスナー コールバック関数に渡されます。matlab.unittest.qualifications.ExceptionEventData

NotifyAccess: private

ListenAccess: public

DiagnosticLoggedlog メソッドの呼び出し時にトリガーされます。LoggedDiagnosticEventData オブジェクトがリスナー コールバック関数に渡されます。matlab.unittest.diagnostics.LoggedDiagnosticEventData

NotifyAccess: private

ListenAccess: public

すべて折りたたむ

テスト クラスを作成して実行し、Figure のプロパティをテストします。

現在のフォルダー内の FigurePropertiesTest.m という名前のファイルで、次のようにして FigurePropertiesTest テスト クラスを作成します。

  • matlab.unittest.TestCase クラスをサブクラス化する

  • テスト対象の Figure を表すプロパティを追加する

  • TestMethodSetupmethods ブロックで各テストのセットアップ コードと破棄コードを追加する

  • Testmethods ブロックでテストを追加する

classdef FigurePropertiesTest < matlab.unittest.TestCase
    properties
        TestFigure
    end

    methods (TestMethodSetup)
        function createFigure(testCase)
            testCase.TestFigure = figure;
            testCase.addTeardown(@close,testCase.TestFigure)
        end
    end

    methods (Test)
        function defaultCurrentPoint(testCase)
            cp = testCase.TestFigure.CurrentPoint;
            testCase.verifyEqual(cp,[0 0], ...
                "Default current point must be [0 0].")
        end

        function defaultCurrentObject(testCase)
            import matlab.unittest.constraints.IsEmpty
            co = testCase.TestFigure.CurrentObject;
            testCase.verifyThat(co,IsEmpty, ...
                "Default current object must be empty.")
        end
    end
end

テスト クラスのテストを実行します。この例では、両方のテストにパスします。

results = runtests("FigurePropertiesTest")
Running FigurePropertiesTest
.
.
Done FigurePropertiesTest
__________
results = 
  1×2 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

Totals:
   2 Passed, 0 Failed, 0 Incomplete.
   1.1899 seconds testing time.

詳細

すべて展開する

ヒント

  • TestCase サブクラスでコンストラクター メソッドまたはデストラクター メソッドを定義することは推奨されません。TestCase のコンストラクター メソッドとデストラクター メソッドはテスト内容と見なされず、検定の実行に使用してはなりません。たとえば、次の SampleTest クラスでは、コンストラクター メソッドと Test メソッドを使用して検定を指定しています。ただし、コンストラクター メソッド内の検定はテスト エラーを生成しません。テスト フレームワークは、testSize メソッド内で実行される検定の結果として、テスト エラーを 1 つしか報告しません。

    classdef SampleTest < matlab.unittest.TestCase
        methods
            function testCase = SampleTest % Constructor method not recommended
                testCase.verifyEqual(1,2)  % Does not produce a test failure
            end
        end
    
        methods (Test)
           function testSize(testCase)
               testCase.verifySize([1 2 3; 4 5 6],[2 4]) % Produces a test failure
           end
        end
    end
    

バージョン履歴

R2013a で導入

すべて展開する