主要内容

verifyThat

R2026b

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

验证值满足指定的约束

描述

verifyThat(testCase,actual,constraint) 验证 actual 是一个满足指定约束的值。如果测试框架显示测试的诊断信息,它只使用该约束提供的诊断。

示例

verifyThat(testCase,actual,constraint,diagnostic) 还将 diagnostic 中的诊断信息与鉴定相关联。使用此语法时,框架会同时显示 constraint 和 diagnostic 提供的诊断信息。

示例

输入参数

全部展开

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

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

(要使测试通过) actual 值必须满足的约束,指定为 matlab.unittest.constraints.Constraint 类的实例。

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

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

示例: "My Custom Diagnostic"

示例: @dir

属性

Sealedtrue

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

示例

全部展开

测试 actual 值是否满足指定的约束。

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

testCase = matlab.unittest.TestCase.forInteractiveUse;

测试 true。验证它是否满足 IsTrue 约束。

import matlab.unittest.constraints.IsTrue
verifyThat(testCase,true,IsTrue)
Verification passed.

测试字符串 "Hello" 和 "hello" 是否相等。此测试失败,因为该比较区分大小写。

import matlab.unittest.constraints.IsEqualTo
verifyThat(testCase,"Hello",IsEqualTo("hello"))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> StringComparator failed.
        --> The strings are not equal.
        
        Actual Value:
            "Hello"
        Expected Value:
            "hello"
    ------------------
    Stack Information:
    ------------------
    In C:\work\TestUsingConstraintsExample.m (TestUsingConstraintsExample) at 20

测试包含空数值数组的元胞数组是否为空。测试失败。

import matlab.unittest.constraints.IsEmpty
verifyThat(testCase,{[]},IsEmpty,"Cell array must be empty.")
Verification failed.
    ----------------
    Test Diagnostic:
    ----------------
    Cell array must be empty.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEmpty failed.
    --> The value must be empty.
    --> The value has a size of [1  1].
    
    Actual Value:
      1×1 cell array
    
        {0×0 double}
    ------------------
    Stack Information:
    ------------------
    In C:\work\TestUsingConstraintsExample.m (TestUsingConstraintsExample) at 26

确认数组不包含任何 NaN 值。

import matlab.unittest.constraints.HasNaN
verifyThat(testCase,[Inf -7+1i],~HasNaN)
Verification passed.

测试一个数值数组是否包含两个元素,并且两个元素都大于 1。

import matlab.unittest.constraints.HasElementCount
import matlab.unittest.constraints.IsGreaterThan
verifyThat(testCase,[3 5],HasElementCount(2) & IsGreaterThan(1))
Verification passed.

提示

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

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

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

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

  • 要在生成的 C/C++ 代码的形式化等效性测试中使用 verifyThat,请将 actual 指定为 matlabtest.coder.MATLABCoderTester (MATLAB Test) 的实例,并将 constraint 指定为 matlabtest.constraints.ExecutionMatchesMATLAB (MATLAB Test) 的实例。

版本历史记录

在 R2013a 中推出