Main Content

失敗したテストの再実行

テストの失敗が不適切なコードや不完全なコードに起因する場合、失敗したテストを素早く便利な方法で再実行することが役立ちます。テスト スイートを実行すると、テスト結果にはテスト スイートおよびテスト ランナーに関する情報が含まれます。結果にテストの失敗が含まれる場合、MATLAB で表示されるテスト結果に、失敗したテストを再実行するためのリンクが含まれます。

Totals:
   1 Passed, 1 Failed (rerun), 0 Incomplete.
   0.25382 seconds testing time.

このリンクによって、テスト コード、つまりテスト対象のコードを修正し、失敗したテストを素早く再実行することができます。ただし、テスト クラスに構造的な変更を行う場合、再実行リンクを使用しても変更点が反映されません。構造的な変更には、テスト メソッドの追加、削除、または名前変更、およびテスト パラメーター プロパティとその値の変更などがあります。この場合、変更を反映させるにはテスト スイート全体を再作成します。

現在の作業フォルダー内に次の関数を作成します。この関数は、二乗と平方根を計算するためのものです。しかし、この例では関数が値の二乗ではなく 3 乗を計算します。

function [x,y] = exampleFunction(n)
    validateattributes(n,{'numeric'},{'scalar'})
    
    x = n^3;     % square (incorrect code, should be n^2)
    y = sqrt(n); % square root
end

ファイル exampleTest.m 内に次のテストを作成します。

function tests = exampleTest
    tests = functiontests(localfunctions);
end

function testSquare(testCase)
    [sqrVal,sqrRootVal] = exampleFunction(3);
    verifyEqual(testCase,sqrVal,9);
end

function testSquareRoot(testCase)
    [sqrVal,sqrRootVal] = exampleFunction(100);
    verifyEqual(testCase,sqrRootVal,10);
end

テスト スイートを作成してテストを実行します。exampleFunction の実装が正しくないため testSquare テストは失敗します。

suite = testsuite('ExampleTest.m');
results = run(suite)
Running exampleTest

================================================================================
Verification failed in exampleTest/testSquare.

    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEqual failed.
    --> The values are not equal using "isequaln".
    --> Failure table:
            Actual    Expected    Error    RelativeError
            ______    ________    _____    _____________
        
              27         9         18            2      
    
    Actual Value:
        27
    Expected Value:
         9

    ------------------
    Stack Information:
    ------------------
    In C:\Work\exampleTest.m (testSquare) at 7
================================================================================
..
Done exampleTest
__________

Failure Summary:

     Name                    Failed  Incomplete  Reason(s)
    =====================================================================
     exampleTest/testSquare    X                 Failed by verification.
    

results = 

  1×2 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

Totals:
   1 Passed, 1 Failed (rerun), 0 Incomplete.
   0.24851 seconds testing time.

exampleFunction 内のコードを更新してコーディングの誤りを修正します。

function [x,y] = exampleFunction(n)
    validateattributes(n,{'numeric'},{'scalar'})
    
    x = n^2;     % square
    y = sqrt(n); % square root
end

コマンド ウィンドウで (rerun) リンクをクリックして、失敗したテストを再実行します。テスト結果を格納する変数が上書きされる場合、失敗したテストを再実行できません。コマンド ウィンドウにリンクが表示されなくなった場合は、プロンプトで results を入力すると表示されます。

Running exampleTest
.
Done exampleTest
__________


ans = 

  TestResult with properties:

          Name: 'exampleTest/testSquare'
        Passed: 1
        Failed: 0
    Incomplete: 0
      Duration: 0.0034
       Details: [1×1 struct]

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

MATLAB は、再実行するテストに関連付けられている TestResult 配列を変数 ans に格納します。resultsexampleTest.m 内のすべてのテストを含む 1 行 2 列の配列、ans は 1 つの失敗したテストの再実行結果を含む 1 行 1 列の配列です。

whos
   Name         Size            Bytes  Class                         Attributes

  ans          1x1               664  matlab.unittest.TestResult              
  results      1x2              1344  matlab.unittest.TestResult              
  suite        1x2                96  matlab.unittest.Test      

失敗したテストをプログラムにより再実行するには、TestResult オブジェクトの Failed プロパティを使用して、フィルター済みのテスト スイートを作成し、実行します。

failedTests = suite([results.Failed]);
result2 = run(failedTests);
Running exampleTest
.
Done exampleTest
__________

パスしたすべてのテストがその後もパスし続けることを確認するには、テスト スイート全体を再実行します。

関連するトピック