Main Content

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

動的にフィルター処理されたテスト

仮定エラーにより "フィルター処理されたテスト" が生成されます。matlab.unittest.TestResult クラスでは、そのようなテストは Incomplete とマークされます。

仮定を使用したテスト内容のフィルター処理ではテストのエラーが生成されないため、デッド テスト コードが生成される可能性があります。これを回避するには、フィルター処理されたテストを監視する必要があります。

テスト メソッド

Test 属性を指定した TestCase メソッド内で仮定エラーが発生した場合、メソッド全体がフィルター処理済みとしてマークされますが、MATLAB® は後続の Test メソッドを実行します。

次のクラスでは、Test ブロックの 1 つのメソッドに仮定エラーがあります。

classdef ExampleTest < matlab.unittest.TestCase
    methods(Test)
        function testA(testCase)
            testCase.verifyTrue(true)
        end
        function testB(testCase)
            testCase.assumeEqual(0,1)
            % remaining test code is not exercised
        end
        function testC(testCase)
            testCase.verifyFalse(true)
        end
    end
end

testB メソッドに仮定エラーがあるため、テストを実行するとテスト フレームワークはテストをフィルター処理し、テストを未完了とマークします。testB の仮定エラーの後、テスト フレームワークは処理を続行し、検証エラーを含む testC を実行します。

ts = matlab.unittest.TestSuite.fromClass(?ExampleTest);
res = ts.run;
Running ExampleTest
.
================================================================================
ExampleTest/testB was filtered.
Details
================================================================================
.
================================================================================
Verification failed in ExampleTest/testC.

    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyFalse failed.
    --> The value must evaluate to "false".
    
    Actual logical:
           1

    ------------------
    Stack Information:
    ------------------
    In C:\work\ExampleTest.m (ExampleTest.testC) at 11
================================================================================
.
Done ExampleTest
__________

Failure Summary:

     Name               Failed  Incomplete  Reason(s)
    ================================================================
     ExampleTest/testB              X       Filtered by assumption.
    ----------------------------------------------------------------
     ExampleTest/testC    X                 Failed by verification.

TestResult を調べてみると、パスしたテスト、失敗したテスト、仮定エラーのため完了していないテストがあることがわかります。

res
res = 

  1×3 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

Totals:
   1 Passed, 1 Failed, 1 Incomplete.
   2.4807 seconds testing time.

テスト フレームワークは、未完了のテストを記録するので、フィルター処理されたテストを監視して実行されていないテスト コードを確認できます。これらのテストについての情報は、TestResult オブジェクト内で確認できます。

res([res.Incomplete])
ans = 

  TestResult with properties:

          Name: 'ExampleTest/testB'
        Passed: 0
        Failed: 0
    Incomplete: 1
      Duration: 2.2578
       Details: [1×1 struct]

Totals:
   0 Passed, 0 Failed, 1 Incomplete.
   2.2578 seconds testing time.

フィルター処理されたテストのみから変更したテスト スイートを作成するには、元のテスト スイートから未完了のテストを選択します。

tsFiltered = ts([res.Incomplete])
tsFiltered = 

  Test with properties:
                  Name: 'ExampleTest/testB'
         ProcedureName: 'testB'
             TestClass: "ExampleTest"
            BaseFolder: 'C:\work'
      Parameterization: [0×0 matlab.unittest.parameters.EmptyParameter]
    SharedTestFixtures: [0×0 matlab.unittest.fixtures.EmptyFixture]
                  Tags: {1×0 cell}

Tests Include:
   0 Parameterizations, 0 Shared Test Fixture Classes, 0 Tags.

メソッドのセットアップおよび破棄コード

TestMethodSetup 属性を指定した TestCase メソッド内で仮定エラーが発生した場合、MATLAB はそのインスタンスで実行する予定であったメソッドをフィルター処理します。テストが TestMethodSetup ブロック内の仮定を使用する場合は、代わりに TestClassSetup ブロック内の仮定を使用してみてください。これは、同じようにクラスのすべての Test メソッドをフィルター処理しますが、詳細レベルが低く、処理効率が向上します。

次の ExampleTest.m 内の TestMethodSetup ブロックのメソッドの 1 つに仮定エラーがあります。

    methods(TestMethodSetup)
        function setupMethod1(testCase)
            testCase.assumeEqual(1,0)
            % remaining test code is not exercised
        end
        function setupMethod2(testCase)
            disp('* Running setupMethod2 *')
            testCase.assertEqual(1,1)
        end
    end

 更新された ExampleTest クラスの定義

テストを実行すると、フレームワークは仮定エラーのない TestMethodSetup ブロック内のすべてのメソッドを完了し、Test ブロック内のすべてのメソッドを未完了とマークします。

ts = matlab.unittest.TestSuite.fromClass(?ExampleTest);
res = ts.run;
Running ExampleTest

================================================================================
ExampleTest/testA was filtered.
    Details
================================================================================
* Running setupMethod2 *
.
================================================================================
ExampleTest/testB was filtered.
    Details
================================================================================
* Running setupMethod2 *
.
================================================================================
ExampleTest/testC was filtered.
    Details
================================================================================
* Running setupMethod2 *
.
Done ExampleTest
__________

Failure Summary:

     Name               Failed  Incomplete  Reason(s)
    ================================================================
     ExampleTest/testA              X       Filtered by assumption.
    ----------------------------------------------------------------
     ExampleTest/testB              X       Filtered by assumption.
    ----------------------------------------------------------------
     ExampleTest/testC              X       Filtered by assumption.

Test メソッドは変更されませんでしたが、TestMethodSetup ブロックに仮定エラーがあるため、3 つはすべてフィルター処理されました。テスト フレームワークは、仮定エラーのない TestMethodSetup ブロックで setupMethod2 などのメソッドを実行します。予想どおり、テスト フレームワークは、setupMethod2 を 3 回、各 Test メソッドの前に 1 回ずつ実行します。

クラスのセットアップおよび破棄コード

TestClassSetup または TestClassTeardown 属性を指定した TestCase メソッド内で仮定エラーが発生した場合、MATLAB は TestCase クラス全体をフィルター処理します。

次の ExampleTest.m 内の TestClassSetup ブロックのメソッドに仮定エラーがあります。

    methods(TestClassSetup)
        function setupClass(testCase)
            testCase.assumeEqual(1,0)
            % remaining test code is not exercised
        end
    end

 更新された ExampleTest クラスの定義

テストを実行すると、フレームワークが TestMethodSetup または Test のメソッドをいずれも実行しないことがわかります。

ts = matlab.unittest.TestSuite.fromClass(?ExampleTest);
res = ts.run;
Running ExampleTest

================================================================================
All tests in ExampleTest were filtered.
    Details
================================================================================

Done ExampleTest
__________

Failure Summary:

     Name               Failed  Incomplete  Reason(s)
    ================================================================
     ExampleTest/testA              X       Filtered by assumption.
    ----------------------------------------------------------------
     ExampleTest/testB              X       Filtered by assumption.
    ----------------------------------------------------------------
     ExampleTest/testC              X       Filtered by assumption.

Test および TestMethodSetup メソッドは変更されませんでしたが、TestClassSetup ブロックに仮定エラーがあるため、すべてがフィルター処理されました。

参考

| |