onFailure
クラス: matlab.unittest.TestCase
名前空間: matlab.unittest
テスト失敗の診断情報を動的に追加
説明
onFailure( はテスト失敗の診断情報を追加します。テストが失敗した場合、テスト フレームワークは診断を実行します。既定では、検証エラー、アサーション エラー、致命的なアサーション エラー、およびキャッチされていない例外の発生時に、これらの診断が実行されます。testcase,failureDiag)
onFailure( は、テスト フレームワークで仮定エラーが発生した場合も診断を実行するかどうかを示します。仮定エラーの発生時にも診断を実行するには、testcase,failureDiag,'IncludingAssumptionFailures',tf)tf を true に設定します。
入力引数
テスト ケースのインスタンス。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'は、検証エラー、アサーション エラー、致命的なアサーション エラーのテストごとに表示されます。これは、addFailureDiagがTestMethodSetupブロック内の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 で導入
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)