Main Content

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

verifyEqual

类: matlab.unittest.qualifications.Verifiable
命名空间: matlab.unittest.qualifications

确认值等于指定值

说明

示例

verifyEqual(testCase,actual,expected) 验证 actual 严格等于 expected。如果 expected 不是 MATLAB® 或 Java® 对象,actualexpected 必须具有相同的类、大小和值才能通过测试。verifyEqual 比较 actualexpected 的方式与 IsEqualTo 约束相同。

示例

verifyEqual(testCase,actual,expected,diagnostic) 还将 diagnostic 中的诊断信息与验证相关联。

示例

verifyEqual(___,Name,Value) 通过一个或多个名称-值参数指定的其他选项确认相等性。需要在上述任一语法中的所有参数之后指定名称-值参数。在 R2021a 及更早版本中,请在 diagnostic 之前指定名称-值参数。

输入参数

全部展开

测试用例,指定为 matlab.unittest.qualifications.Verifiable 对象。由于 matlab.unittest.TestCase 类会子类化 matlab.unittest.qualifications.Verifiable 并继承其方法,因此 testCase 通常是 matlab.unittest.TestCase 对象。

要测试的值,指定为任何数据类型的值。

预期值,指定为任意数据类型的值。

当验证通过或失败时显示的诊断信息,指定为字符串数组、字符数组、函数句柄或 matlab.automation.diagnostics.Diagnostic 对象数组。

根据测试运行器的配置,测试框架可能会在验证通过或失败时显示诊断信息。默认情况下,框架仅在鉴定失败时显示诊断信息。您可以通过自定义测试运行器来覆盖默认行为。例如,使用 DiagnosticsOutputPlugin 实例来显示失败和通过事件诊断信息。

示例: "My Custom Diagnostic"

示例: @dir

名称-值参数

将可选的参数对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参数名称,Value 是对应的值。名称-值参数必须出现在其他参数之后,但参数对组的顺序无关紧要。

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

示例: verifyEqual(testCase,1.5,2,"AbsTol",1) 验证 actual 值 1.5 和 expected 值 2 之间的差是否在 1 以内。

绝对误差,指定为一个数值数组。AbsTolexpected 的大小必须相同或兼容。有关兼容数组的详细信息,请参阅基本运算的兼容数组大小

容差仅应用于相同数据类型的值。要满足绝对误差,abs(expected-actual) <= AbsTol 必须为 true

相对误差,指定为数值数组。RelTolexpected 的大小必须相同或兼容。有关兼容数组的详细信息,请参阅基本运算的兼容数组大小

容差仅应用于相同数据类型的值。要满足相对误差,abs(expected-actual) <= RelTol.*abs(expected) 必须为 true

属性

Sealedtrue

要了解方法的属性,请参阅方法属性

示例

全部展开

如果数值属于同一类,且大小、复/实性和稀疏相等,则这些数值相等。

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

testCase = matlab.unittest.TestCase.forInteractiveUse;

验证数值是否等于它自身。

verifyEqual(testCase,5,5)
Verification passed.

比较不同大小的值。测试失败。

verifyEqual(testCase,[5 5],5)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEqual failed.
    --> Sizes do not match.
        
        Actual size:
             1     2
        Expected size:
             1     1
    
    Actual Value:
         5     5
    Expected Value:
         5
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareNumericValuesExample.m (CompareNumericValuesExample) at 18

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

testCase = matlab.unittest.TestCase.forInteractiveUse;

比较不同类的两个数值。测试失败。

verifyEqual(testCase,int8(5),int16(5),"Classes must match.")
Verification failed.
    ----------------
    Test Diagnostic:
    ----------------
    Classes must match.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEqual failed.
    --> Classes do not match.
        
        Actual Class:
            int8
        Expected Class:
            int16
    
    Actual Value:
      int8
    
       5
    Expected Value:
      int16
    
       5
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareClassesExample.m (CompareClassesExample) at 12

相等元胞数组的元素必须在类、大小和值上匹配。

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

testCase = matlab.unittest.TestCase.forInteractiveUse;

将一个元胞数组与其自身进行比较。测试通过。

verifyEqual(testCase,{'cell',struct,5},{'cell',struct,5})
Verification passed.

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

testCase = matlab.unittest.TestCase.forInteractiveUse;

测试 actual 值 1.5 是否等于 expected 值 2。测试失败。

verifyEqual(testCase,1.5,2)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEqual failed.
    --> The numeric values are not equal using "isequaln".
    --> Failure table:
            Actual    Expected    Error    RelativeError
            ______    ________    _____    _____________
        
             1.5         2        -0.5         -0.25    
    
    Actual Value:
       1.500000000000000
    Expected Value:
         2
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareValuesUsingNumericTolerancesExample.m (CompareValuesUsingNumericTolerancesExample) at 13

验证 actual 值和 expected 值之间的差是否在 1 以内。

verifyEqual(testCase,1.5,2,"AbsTol",1)
Verification passed.

检验 actual 值和 expected 值之间的差是否小于 10%。测试失败。

verifyEqual(testCase,1.5,2, ...
    "Difference must be within relative tolerance.","RelTol",0.1)
Verification failed.
    ----------------
    Test Diagnostic:
    ----------------
    Difference must be within relative tolerance.
    ---------------------
    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
            ______    ________    _____    _____________    _________________
        
             1.5         2        -0.5         -0.25               0.1       
    
    Actual Value:
       1.500000000000000
    Expected Value:
         2
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareValuesUsingNumericTolerancesExample.m (CompareValuesUsingNumericTolerancesExample) at 23

提示

  • verifyEqual 方法非常方便。例如,verifyEqual(testCase,actual,expected) 在功能上等效于以下代码。

    import matlab.unittest.constraints.IsEqualTo
    testCase.verifyThat(actual,IsEqualTo(expected))

    同样,verifyEqual(testCase,actual,expected,"AbsTol",abstol,"RelTol",reltol) 在功能上等效于以下代码。

    import matlab.unittest.constraints.IsEqualTo
    import matlab.unittest.constraints.AbsoluteTolerance
    import matlab.unittest.constraints.RelativeTolerance
    testCase.verifyThat(actual,IsEqualTo(expected, ...
        'Within',AbsoluteTolerance(abstol) | RelativeTolerance(reltol)))

    当直接通过 verifyThat 使用 IsEqualToAbsoluteToleranceRelativeTolerance 约束时,可利用更多功能。

  • 使用此确认在不引发异常的条件下生成和记录失败。由于确认不会引发异常,因此即使出现确认失败的情形,依然会完成所有的测试内容。通常,确认指的是对单元测试的主要验证,因为这些确认一般不要求提前从测试中退出。使用其他验证类型来测试是否违反先决条件或测试安装是否正确:

    • 使用断言验证确保测试环境满足无论如何都不会导致测试失败的先决条件。假设失败会生成已过滤的测试,且测试框架会将测试设为 Incomplete。有关详细信息,请参阅 matlab.unittest.qualifications.Assumable

    • 当失败条件导致当前测试内容的剩余部分都失效,但不会阻止后续测试正确执行时,使用断言验证。断言点处的失败会将当前测试展现为 FailedIncomplete。有关详细信息,请参阅 matlab.unittest.qualifications.Assertable

    • 使用致命断言验证在失败时中止测试会话。当失败涉及根本以致继续测试已经没有意义时,这些验证非常有用。当脚手架拆解不能正确还原环境状态,适合中止测试并启动一个新会话时,致命断言也很有用。有关详细信息,请参阅 matlab.unittest.qualifications.FatalAssertable

版本历史记录

在 R2013a 中推出

全部展开