Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

matlab.unittest.diagnostics.StringDiagnostic 类

包: matlab.unittest.diagnostics
超类: matlab.unittest.diagnostics.Diagnostic

使用字符串的诊断结果

描述

matlab.unittest.diagnostics.StringDiagnostic 类定义使用字符串的诊断结果。如果在构造时已知诊断信息,请使用 StringDiagnostic 类来显示该诊断信息。

使用 verifyThat 等验证方法时,您可以将测试诊断直接指定为字符串。在这种情况下,测试框架会自动构造 StringDiagnostic 对象。

matlab.unittest.diagnostics.StringDiagnostic 类是 handle 类。

创建对象

描述

示例

diag = matlab.unittest.diagnostics.StringDiagnostic(diagnosticText) 创建一个 StringDiagnostic 对象并设置其 DiagnosticText 属性。

属性

全部展开

用于生成诊断信息的文本,指定为字符向量或字符串数组,并存储为字符向量。

DiagnosticText 属性为诊断的使用方(例如测试框架)提供了一种传达实际诊断信息的方法。

属性:

GetAccess
public
SetAccess
protected

方法

全部展开

示例

全部折叠

创建一个诊断结果,它会在测试失败时显示为字符串。

首先,导入此示例中使用的类。

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.diagnostics.StringDiagnostic

创建一个供交互测试的测试用例。

testCase = TestCase.forInteractiveUse;

使用 StringDiagnostic 对象显示测试失败时的诊断信息。当测试失败时,StringDiagnostic 对象显示指定的字符串。

testCase.verifyThat(1,IsEqualTo(2), ...
    StringDiagnostic("Actual must be equal to expected."))
Verification failed.
    ----------------
    Test Diagnostic:
    ----------------
    Actual must be equal to expected.
    ---------------------
    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

或者,测试框架可以通过 verifyThat 方法的字符串输入创建一个 StringDiagnostic 对象。

testCase.verifyThat(1,IsEqualTo(2), ...
    "Actual must be equal to expected.")
Verification failed.
    ----------------
    Test Diagnostic:
    ----------------
    Actual must be equal to expected.
    ---------------------
    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

测试框架根据需要构造 StringDiagnostic 对象,通常只在测试失败时才构造。

版本历史记录

在 R2013a 中推出