matlab.unittest.diagnostics.ConstraintDiagnostic クラス
名前空間: matlab.unittest.diagnostics
スーパークラス: matlab.automation.diagnostics.Diagnostic
制約に共通するフィールドをもつ診断
説明
matlab.unittest.diagnostics.ConstraintDiagnostic クラスは、制約に共通するフィールドをもつ診断を提供します。これらのフィールドには、説明、条件、実際の値、期待される値が含まれます。詳細については、診断フィールドを参照してください。
ConstraintDiagnostic クラスは、カスタム制約で生成される診断に共通の外観を追加するための便利な方法を提供します。ConstraintDiagnostic オブジェクトを使用して、Constraint サブクラスの getDiagnosticFor メソッドや BooleanConstraint サブクラスの getNegativeDiagnosticFor メソッドを実装できます。
matlab.unittest.diagnostics.ConstraintDiagnostic クラスは handle クラスです。
作成
説明
diag = matlab.unittest.diagnostics.ConstraintDiagnostic で ConstraintDiagnostic オブジェクトが作成されます。その後、このオブジェクトのプロパティを設定して制約診断をカスタマイズできます。
プロパティ
次のプロパティに加え、ConstraintDiagnostic クラスは Diagnostic クラスから Artifacts プロパティと DiagnosticText プロパティを継承します。
説明
総合的な診断情報。string スカラーまたは文字ベクトルとして指定します。
属性:
GetAccess | public |
SetAccess | public |
Description プロパティのテキストを表示するオプション。数値または logical 0 (false) または 1 (true) として指定します。既定では、説明は制約に表示されません。
属性:
GetAccess | public |
SetAccess | public |
条件
書式設定された条件リスト。文字ベクトルとして返されます。フレームワークで診断を表示する際、各条件は新しい行から始まり、矢印 (-->) 区切り記号から開始されます。
条件はテストの失敗の原因に固有の情報を含み、"サブ診断" として機能します。条件は addCondition および addConditionsFrom メソッドを使用してリストに追加します。
属性:
GetAccess | public |
SetAccess | private |
Conditions プロパティに格納された条件リストの条件の数。非負の整数スカラーとして返されます。
属性:
GetAccess | public |
SetAccess | private |
データ型: double
Conditions プロパティの条件リストを表示するオプション。数値または logical 0 (false) または 1 (true) として指定します。既定では、条件リストは制約に表示されません。
属性:
GetAccess | public |
SetAccess | public |
実際の値
テストする実際の値。任意のデータ型の値として指定します。
属性:
GetAccess | public |
SetAccess | public |
実際の値のヘッダー情報。string スカラーまたは文字ベクトルとして指定します。
属性:
GetAccess | public |
SetAccess | public |
実際の値とそのヘッダー情報を表示するオプション。数値または logical 0 (false) または 1 (true) として指定します。既定では、実際の値は制約に表示されません。
属性:
GetAccess | public |
SetAccess | public |
期待される値
期待される値 (該当する場合)。任意のデータ型の値として指定します。
属性:
GetAccess | public |
SetAccess | public |
期待される値のヘッダー情報。string スカラーまたは文字ベクトルとして指定します。
属性:
GetAccess | public |
SetAccess | public |
期待される値とそのヘッダー情報を表示するオプション。数値または logical 0 (false) または 1 (true) として指定します。既定では、期待される値は制約に表示されません。
属性:
GetAccess | public |
SetAccess | public |
メソッド
次のメソッドに加え、ConstraintDiagnostic クラスは Diagnostic クラスから diagnose メソッドと matlab.automation.diagnostics.Diagnostic.join メソッドを継承します。
addCondition |
条件 入力引数
|
addConditionsFrom |
別の 入力引数
|
matlab.unittest.diagnostics.ConstraintDiagnostic.getDisplayableString |
入力引数
出力引数
|
次の保護されたメソッドを ConstraintDiagnostic サブクラスでオーバーライドして、診断にフィールドをさらに追加できます。フレームワークでは、これらのメソッドを呼び出してフィールドを取得し、それらを診断結果内のメソッドで指定される位置に挿入します。オーバーライドしない場合、各メソッドは空の文字ベクトルを返します。
getPreDescriptionString |
説明の前に表示されるテキストを返します。このメソッドをオーバーライドして、 入力引数
出力引数
|
getPostDescriptionString |
説明の後に表示されるテキストを返します。このメソッドをオーバーライドして、 入力引数
出力引数
|
getPostConditionsString |
条件リストの後に表示されるテキストを返します。このメソッドをオーバーライドして、 入力引数
出力引数
|
getPostActValString |
実際の値の後に表示されるテキストを返します。このメソッドをオーバーライドして、 入力引数
出力引数
|
getPostExpValString |
期待される値の後に表示されるテキストを返します。このメソッドをオーバーライドして、 入力引数
出力引数
|
例
値が期待される値と同じサイズであるかどうかを判定するカスタム制約を作成します。制約の診断情報を生成するために、その getDiagnosticFor メソッドを ConstraintDiagnostic クラスを使用して実装します。
現在のフォルダー内のファイルに、matlab.unittest.constraints.Constraint から派生させた IsSameSizeAs という名前のクラスを作成し、satisfiedBy メソッドおよび getDiagnosticFor メソッドを実装します。getDiagnosticFor メソッドの実装には、ConstraintDiagnostic クラスのインスタンスを使用します。ConstraintDiagnostic クラスは、パスしたテストと失敗したテスト用に各種の診断フィールドをカスタマイズするための便利な方法を提供します。
classdef IsSameSizeAs < matlab.unittest.constraints.Constraint properties (SetAccess=immutable) ValueWithExpectedSize end methods function constraint = IsSameSizeAs(value) constraint.ValueWithExpectedSize = value; end function tf = satisfiedBy(constraint,actual) tf = constraint.sizeMatchesExpected(actual); end function diagnostic = getDiagnosticFor(constraint,actual) if constraint.sizeMatchesExpected(actual) diagnostic = diagnosticForPassingTest; else diagnostic = diagnosticForFailingTest(constraint,actual); end end end methods (Access=private) function tf = sizeMatchesExpected(constraint,actual) tf = isequal(size(actual), ... size(constraint.ValueWithExpectedSize)); end function diag = diagnosticForPassingTest(~,~) import matlab.unittest.diagnostics.ConstraintDiagnostic diag = ConstraintDiagnostic; diag.DisplayDescription = true; diag.Description = "IsSameSizeAs passed."; end function diag = diagnosticForFailingTest(constraint,actual) import matlab.unittest.diagnostics.ConstraintDiagnostic diag = ConstraintDiagnostic; diag.DisplayDescription = true; diag.Description = "IsSameSizeAs failed."; diag.DisplayConditions = true; diag.addCondition("Sizes did not match.") diag.DisplayActVal = true; diag.ActValHeader = "Actual Size:"; diag.ActVal = size(actual); diag.DisplayExpVal = true; diag.ExpValHeader = "Expected Size:"; diag.ExpVal = size(constraint.ValueWithExpectedSize); end end end
対話型テスト用にテスト ケースを作成します。
testCase = matlab.unittest.TestCase.forInteractiveUse;
失敗のケースをテストします。補助メソッド diagnosticForFailingTest で実装された制約診断がフレームワークで表示されます。
testCase.verifyThat(zeros(5),IsSameSizeAs(ones(1,5)))
Verification failed.
---------------------
Framework Diagnostic:
---------------------
IsSameSizeAs failed.
--> Sizes did not match.
Actual Size:
5 5
Expected Size:
1 5診断の説明フィールドの前にテキストを挿入する制約診断を作成します。
現在のフォルダー内のファイルに、matlab.unittest.diagnostics.ConstraintDiagnostic から派生させた CustomConstraintDiagnostic という名前のクラスを作成します。オプションである診断の説明フィールドの前にテキストを挿入するために、カスタム診断クラスがそのスーパークラスから継承する getPreDescriptionString メソッドを実装します。
classdef CustomConstraintDiagnostic < ... matlab.unittest.diagnostics.ConstraintDiagnostic properties (SetAccess=immutable) Context end methods function diag = CustomConstraintDiagnostic(context) arguments context (1,1) string = "Test Outcome Information" end diag.Context = context; end end methods (Access=protected) function str = getPreDescriptionString(diag) str = diag.Context; end end end
この例で使用するクラスをインポートします。
import matlab.unittest.TestCase import matlab.unittest.constraints.IsEqualTo
CustomConstraintDiagnostic クラスをインスタンス化して制約診断を作成します。
diagnostic = CustomConstraintDiagnostic;
diagnostic.DisplayDescription = true;
diagnostic.Description = "My Custom Diagnostic";実際には、カスタム制約の実装には制約診断を使用するのが一般的です。この例では、簡略化するために、対話型テスト用のテスト ケースを作成し、テストの失敗時に CustomConstraintDiagnostic オブジェクトを使用して診断情報を表示します。指定した説明と説明の前に挿入される既定のテキストの両方がフレームワークで表示されます。
testCase = TestCase.forInteractiveUse; testCase.verifyThat(1,IsEqualTo(2),diagnostic)
Verification failed.
----------------
Test Diagnostic:
----------------
Test Outcome Information
My Custom Diagnostic
---------------------
Framework Diagnostic:
---------------------
IsEqualTo failed.
--> NumericComparator failed.
--> The numeric values are not equal using "isequaln".
--> Failure table:
Actual Expected Error RelativeError
______ ________ _____ _____________
1 2 -1 -0.5
Actual Value:
1
Expected Value:
2詳細
制約診断は、次のテキスト フィールドで構成されます。表示される順序もここに示すとおりです。
診断 — 総合的な診断情報。
条件 — 失敗の原因を記述する書式設定された条件リスト。各条件は新しい行から始まり、矢印 (
-->) 区切り記号から開始されます。実際の値 — 制約に関連する実際の値。適切に表示するために、テキストが切り捨てられたり書式設定されたりすることがあります。
期待される値 — 制約に関連する期待される値 (該当する場合)。
ConstraintDiagnostic クラスは、これらのフィールドを使用して制約診断を構成するために、matlab.automation.diagnostics.Diagnostic から diagnose メソッドを継承して実装します。
バージョン履歴
R2013a で導入
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)