Main Content

onFailure

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

テスト失敗の診断情報を動的に追加

説明

onFailure(testcase,failureDiag) はテスト失敗の診断情報を追加します。テストが失敗した場合、テスト フレームワークは診断を実行します。既定では、検証エラー、アサーション エラー、致命的なアサーション エラー、およびキャッチされていない例外の発生時に、これらの診断が実行されます。

onFailure(testcase,failureDiag,'IncludingAssumptionFailures',tf) は、テスト フレームワークで仮定エラーが発生した場合も診断を実行するかどうかを示します。仮定エラーの発生時にも診断を実行するには、tftrue に設定します。

入力引数

すべて展開する

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

エラーの発生時に表示する診断情報。文字ベクトル、string 配列、関数ハンドル、または matlab.automation.diagnostics.Diagnostic インスタンスの配列として指定します。

例: @() disp('Failure Detected')

例: matlab.unittest.diagnostics.ScreenshotDiagnostic

仮定エラーへの反応。false (logical の 0) または true (logical の 1) として指定します。この既定値は false で、テスト フレームワークは、検証エラー、アサーション エラー、致命的なアサーション エラー、およびキャッチされていない例外の発生時に診断を実行します。ただし、フレームワークは仮定エラーの発生時に診断を実行しません。仮定エラーの発生時に追加の診断を実行するには、この値を true に指定します。

すべて展開する

現在のフォルダーに SampleOnFailureTest テスト クラスを作成します。

classdef SampleOnFailureTest < matlab.unittest.TestCase
    methods (TestMethodSetup)
        function addFailureDiag(testCase)
            testCase.onFailure('Failure Detected')
        end
    end
    methods (Test)
        function verificationFailTest(testCase)
            testCase.onFailure(@()disp(datetime))
            testCase.verifyEqual(42,13)
        end
        function passingTest(testCase)
            testCase.assertTrue(true)
        end
        function assumptionFailTest(testCase)
            testCase.assumeEmpty(rand(2))
        end
        function assertionFailTest(testCase)
            act = randi(100,1,15);
            floor = randi(100,1,15);
            f = figure;
            plot(1:length(act),act,1:length(floor),floor)
            legend('actual','floor')
            testCase.addTeardown(@close,f)
            import matlab.unittest.diagnostics.FigureDiagnostic
            testCase.onFailure(FigureDiagnostic(f,'Formats','png'))
            testCase.assertGreaterThan(act,floor)
        end
    end
end

コマンド プロンプトで、テストを実行します。SampleOnFailureTest クラスには、以下の結果があります。

  • 診断メッセージ 'Failure Detected' は、検証エラー、アサーション エラー、致命的なアサーション エラーのテストごとに表示されます。これは、addFailureDiagTestMethodSetup ブロック内の onFailure を呼び出すためです。

  • verificationFailTest テストは、エラーの発生時に現在の日付と時刻を表示する別の診断情報を追加します。

  • assumptionFailTest テストは仮定により失敗します。したがって、'Failure Detected' メッセージは表示されません。

  • assertionFailTest テストはデータをプロットします。テストが失敗すると、テスト フレームワークはそのプロットを保存します。

results = runtests('SampleOnFailureTest');
Running SampleOnFailureTest

================================================================================
Verification failed in SampleOnFailureTest/verificationFailTest.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEqual failed.
    --> The numeric values are not equal using "isequaln".
    --> Failure table:
            Actual    Expected    Error     RelativeError  
            ______    ________    _____    ________________
        
              42         13        29      2.23076923076923
    
    Actual Value:
        42
    Expected Value:
        13
    ----------------------
    Additional Diagnostic:
    ----------------------
    Failure Detected
    ----------------------
    Additional Diagnostic:
    ----------------------
       05-Feb-2021 12:13:36
    
    
    ------------------
    Stack Information:
    ------------------
    In C:\work\MyExamples\SampleOnFailureTest.m (SampleOnFailureTest.verificationFailTest) at 10
================================================================================
..
================================================================================
SampleOnFailureTest/assumptionFailTest was filtered.
================================================================================
.
================================================================================
Assertion failed in SampleOnFailureTest/assertionFailTest and it did not run to completion.
    ---------------------
    Framework Diagnostic:
    ---------------------
    assertGreaterThan failed.
    --> Each element must be greater than each corresponding element of the minimum value array.
        
        Failing Indices:
             3     8    10    11    13    15
    
    Actual Value:
      Columns 1 through 13
    
        44    59    29     8    93    66    31    55    84    52    18    97    23
    
      Columns 14 through 15
    
        95     2
    Minimum Value (Exclusive):
      Columns 1 through 13
    
         3    23    32     5    43     1    17    76    62    91    51    33    26
    
      Columns 14 through 15
    
        11   100
    ----------------------
    Additional Diagnostic:
    ----------------------
    Failure Detected
    ----------------------
    Additional Diagnostic:
    ----------------------
    Figure saved to:
    --> C:\work\Temp\Figure_20a2887e-85de-4398-8aa4-26ef8d3d921d.png
    ------------------
    Stack Information:
    ------------------
    In C:\work\MyExamples\SampleOnFailureTest.m (SampleOnFailureTest.assertionFailTest) at 27
================================================================================
.
Done SampleOnFailureTest
__________

Failure Summary:

     Name                                      Failed  Incomplete  Reason(s)
    =======================================================================================
     SampleOnFailureTest/verificationFailTest    X                 Failed by verification.
    ---------------------------------------------------------------------------------------
     SampleOnFailureTest/assumptionFailTest                X       Filtered by assumption.
    ---------------------------------------------------------------------------------------
     SampleOnFailureTest/assertionFailTest       X         X       Failed by assertion.

ヒント

  • クラス内の各テストに診断を追加するには、TestMethodSetup ブロック内のメソッドから onFailure を呼び出します。

バージョン履歴

R2017b で導入