onFailure
クラス: matlab.unittest.TestCase
パッケージ: matlab.unittest
テスト失敗の診断情報を動的に追加
説明
onFailure(
はテスト失敗の診断情報を追加します。テストが失敗した場合、テスト フレームワークは診断を実行します。既定では、検証エラー、アサーション エラー、致命的なアサーション エラー、およびキャッチされていない例外の発生時に、これらの診断が実行されます。testcase
,failureDiag
)
onFailure(
は、テスト フレームワークで仮定エラーが発生した場合も診断を実行するかどうかを示します。仮定エラーの発生時にも診断を実行するには、testcase
,failureDiag
,'IncludingAssumptionFailures',tf)tf
を true
に設定します。
入力引数
testCase
— テスト ケースのインスタンス
matlab.unittest.TestCase
インスタンス
テスト ケースのインスタンス。matlab.unittest.TestCase
オブジェクトとして指定します。
failureDiag
— 診断情報
文字ベクトル | string 配列 | 関数ハンドル | matlab.automation.diagnostics.Diagnostic
インスタンスの配列
エラーの発生時に表示する診断情報。文字ベクトル、string 配列、関数ハンドル、または matlab.automation.diagnostics.Diagnostic
インスタンスの配列として指定します。
例: @() disp('Failure Detected')
例: matlab.unittest.diagnostics.ScreenshotDiagnostic
tf
— 仮定エラーへの反応
false
(既定値) | true
仮定エラーへの反応。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 コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは MATLAB コマンドをサポートしていません。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)