matlab.unittest.plugins.StopOnFailuresPlugin クラス
名前空間: matlab.unittest.plugins
スーパークラス: matlab.unittest.plugins.TestRunnerPlugin
テストのエラーをデバッグするプラグイン
説明
matlab.unittest.plugins.StopOnFailuresPlugin クラスは、テストのエラーをデバッグするプラグインを定義します。StopOnFailuresPlugin をもつテスト ランナーで検定エラーやキャッチされないエラーが発生すると、テストの実行が一時停止され、MATLAB® がデバッグ モードになります。dbstep、dbcont、dbquit などの MATLAB デバッグ コマンドを使用して、テストの失敗やエラーの原因を調査できます。
matlab.unittest.plugins.StopOnFailuresPlugin クラスは handle クラスです。
作成
説明
p = matlab.unittest.plugins.StopOnFailuresPlugin は、テストのエラーをデバッグするプラグインを作成します。
p = matlab.unittest.plugins.StopOnFailuresPlugin("IncludingAssumptionFailures",tf) は IncludeAssumptionFailures プロパティを tf に設定します。この構文は、仮定エラーに反応するかどうかを示すために使用します。既定では、StopOnFailuresPlugin は、キャッチされないエラー、検証エラー、アサーション エラー、および致命的なアサーション エラーにのみ反応します。ただし、IncludingAssumptionFailures が true として指定されている場合、プラグインは仮定エラーにも反応します。
プロパティ
仮定エラーに反応するかどうか。数値または logical 0 (false) または 1 (true) として指定し、logical 値として格納されます。値が true の場合、プラグインは仮定エラーに反応します。値が false の場合、プラグインは仮定エラーを無視します。
属性:
GetAccess | public |
SetAccess | private |
例
テスト ランナーに StopOnFailuresPlugin インスタンスを追加して、テスト エラーの原因を調査します。
現在のフォルダーに ExampleTest テスト クラスを作成します。
classdef ExampleTest < matlab.unittest.TestCase methods (Test) function testOne(testCase) % Test fails act = 3.1416; exp = pi; testCase.verifyEqual(act,exp) end function testTwo(testCase) % Test does not complete testCase.assumeEqual(5,4) end end end
コマンド プロンプトで ExampleTest からテスト スイートを作成してテストを実行します。テスト クラスの検定の結果、最初のテストは失敗し、2 番目のテストは完了しません。
import matlab.unittest.plugins.StopOnFailuresPlugin suite = testsuite("ExampleTest"); runner = testrunner("textoutput"); results = runner.run(suite);
Running ExampleTest
================================================================================
Verification failed in ExampleTest/testOne.
---------------------
Framework Diagnostic:
---------------------
verifyEqual failed.
--> The numeric values are not equal using "isequaln".
--> Failure table:
Actual Expected Error RelativeError
______ ________________ ____________________ ____________________
3.1416 3.14159265358979 7.34641020683213e-06 2.33843499679617e-06
Actual Value:
3.141600000000000
Expected Value:
3.141592653589793
------------------
Stack Information:
------------------
In C:\work\ExampleTest.m (ExampleTest.testOne) at 6
================================================================================
.
================================================================================
ExampleTest/testTwo was filtered.
================================================================================
.
Done ExampleTest
__________
Failure Summary:
Name Failed Incomplete Reason(s)
==================================================================
ExampleTest/testOne X Failed by verification.
------------------------------------------------------------------
ExampleTest/testTwo X Filtered by assumption.テスト ランナーに StopOnFailuresPlugin インスタンスを追加し、テストを再度実行します。テストの実行中にエラーが発生すると、MATLAB はエラーの原因箇所でデバッグ モードになります。
runner.addPlugin(StopOnFailuresPlugin) result = runner.run(suite);
Running ExampleTest
================================================================================
Verification failed in ExampleTest/testOne.
---------------------
Framework Diagnostic:
---------------------
verifyEqual failed.
--> The numeric values are not equal using "isequaln".
--> Failure table:
Actual Expected Error RelativeError
______ ________________ ____________________ ____________________
3.1416 3.14159265358979 7.34641020683213e-06 2.33843499679617e-06
Actual Value:
3.141600000000000
Expected Value:
3.141592653589793
------------------
Stack Information:
------------------
In C:\work\ExampleTest.m (ExampleTest.testOne) at 6
================================================================================
Test execution paused due to failure. To terminate the test run, use dbquit. To continue, use dbcont.関数 whos を使用してワークスペースの変数を調べ、テスト エラーの原因を調査します。
whos
Name Size Bytes Class Attributes act 1x1 8 double exp 1x1 8 double testCase 1x1 8 ExampleTest
相対許容誤差を 100*eps にして、失敗したテストを再度実行してみます。指定した許容誤差でもテストに失敗します。
testCase.verifyEqual(act,exp,"RelTol",100*eps)================================================================================
Verification failed in ExampleTest/testOne.
---------------------
Framework Diagnostic:
---------------------
verifyEqual failed.
--> The numeric values are not equal using "isequaln".
--> The error was not within relative tolerance.
--> Failure table:
Actual Expected Error RelativeError RelativeTolerance
______ ________________ ____________________ ____________________ ____________________
3.1416 3.14159265358979 7.34641020683213e-06 2.33843499679617e-06 2.22044604925031e-14
Actual Value:
3.141600000000000
Expected Value:
3.141592653589793
------------------
Stack Information:
------------------
In C:\work\ExampleTest.m (ExampleTest.testOne) at 6
================================================================================絶対許容誤差を 0.001 にして、失敗したテストを再度実行してみます。この絶対許容誤差では、失敗したテストにパスします。
testCase.verifyEqual(act,exp,"AbsTol",0.001)dbquit を使用して、テストの実行を終了します。dbcont を使用すると、デバッグ モードを終了して残りのテストを実行することもできます。
dbquit
ExampleTest クラスの testTwo のように、仮定により失敗するテストでデバッグ モードにするには、プラグインの作成時に IncludingAssumptionFailures を true として指定します。(テスト ランナーに追加する StopOnFailuresPlugin インスタンスは、必ず 1 つだけにしてください。StopOnFailuresPlugin インスタンスを複数追加すると、予期しない動作になる可能性があります。)
テストを実行すると、MATLAB が testOne と testTwo の両方でデバッグ モードになります。
runner.addPlugin(StopOnFailuresPlugin( ... "IncludingAssumptionFailures",true)) result = runner.run(suite);
StopOnFailuresPlugin インスタンスを使用してテストのエラーをデバッグします。
現在のフォルダーに ExampleTest テスト クラスを作成します。testTwo メソッドに無効な関数呼び出しを含めて、テストにエラーを挿入します。
classdef ExampleTest < matlab.unittest.TestCase methods (Test) function testOne(testCase) % Test passes act = round(pi); exp = 3; testCase.verifyEqual(act,exp) end function testTwo(testCase) % Test throws an error act = cosine(0); % Invalid function call exp = 1; testCase.verifyEqual(act,exp) end end end
コマンド プロンプトで ExampleTest からテスト スイートを作成してテストを実行します。エラーの結果として、2 番目のテストは失敗し、完了しません。
import matlab.unittest.plugins.StopOnFailuresPlugin suite = testsuite("ExampleTest"); runner = testrunner("textoutput"); results = runner.run(suite);
Running ExampleTest
.
================================================================================
Error occurred in ExampleTest/testTwo and it did not run to completion.
---------
Error ID:
---------
'MATLAB:UndefinedFunction'
--------------
Error Details:
--------------
Undefined function 'cosine' for input arguments of type 'double'.
Error in ExampleTest/testTwo (line 9)
act = cosine(0); % Invalid function call
================================================================================
.
Done ExampleTest
__________
Failure Summary:
Name Failed Incomplete Reason(s)
====================================================
ExampleTest/testTwo X X Errored.ランナーに StopOnFailuresPlugin インスタンスを追加し、テストを再度実行します。テストの実行中にエラーがスローされると、MATLAB はエラーの原因箇所でデバッグ モードになります。
runner.addPlugin(StopOnFailuresPlugin) result = runner.run(suite);
Running ExampleTest .9 act = cosine(0); % Invalid function call K>>
正しい関数名 (つまり、cos) を使用してエラーを修正します。テストを再度実行すると、両方ともパスします。
バージョン履歴
R2013b で導入StopOnFailuresPlugin インスタンスが含まれているテスト ランナーでキャッチされていないエラーが検出されると、MATLAB はエラーの原因箇所でデバッグ モードになります。これにより、デバッグ コマンドを使用してエラーの原因を調査できます。
以前のリリースでは、プラグインがエラーを報告するためにテストの実行を停止している間は、エラーによってスタックが中断されるため、デバッグ機能は制限されます。
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)