Main Content

matlab.unittest.constraints.Eventually 类

命名空间: matlab.unittest.constraints
超类: matlab.unittest.constraints.Constraint

测试函数是否以异步方式满足约束

描述

matlab.unittest.constraints.Eventually 类提供一个约束,该约束在超时期限内轮询函数句柄是否满足给定约束。

matlab.unittest.constraints.Eventually 类是一个 handle 类。

创建对象

描述

示例

c = matlab.unittest.constraints.Eventually(constraint) 创建一个约束,该约束轮询函数句柄是否满足 constraint。如果在 20 秒的超时期限内,函数句柄产生了满足 constraint 的值,则满足约束 c

Eventually 等待函数句柄满足 constraint 时,测试框架更新图窗并处理任何挂起的回调。

示例

c = matlab.unittest.constraints.Eventually(constraint,"WithTimeoutOf",timeout) 创建一个约束,该约束在指定的超时期限内轮询 constraint 是否得到满足。

输入参量

全部展开

必须以异步方式满足的约束,指定为 matlab.unittest.constraints.Constraint 对象。

以秒为单位的超时期限,指定为非负数值标量。

此参量设置 Timeout 属性。

数据类型: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

属性

全部展开

函数句柄产生的最后一个输出,以任何数据类型的值形式返回。

属性:

GetAccess
public
SetAccess
private

以秒为单位的超时期限,以非负数值标量形式返回。

此属性由 timeout 输入参量设置。

属性:

GetAccess
public
SetAccess
immutable

示例

全部折叠

使用 Eventually 约束测试鉴定是否最终通过。

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.Eventually
import matlab.unittest.constraints.IsGreaterThan
import matlab.unittest.constraints.IsLessThan

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

testCase = TestCase.forInteractiveUse;

验证在 20 秒内自上次调用 tic 以来,调用 toc 返回的值大于 10。在此测试中,Eventually 约束重复调用 toc,直至满足约束或者所用时间超过时限。IsGreaterThan。如果您调用 tic,然后等待 10 秒以上再调用 verifyThat,测试会立即通过,因为 toc 已产生大于 10 的值。

tic
testCase.verifyThat(@toc,Eventually(IsGreaterThan(10)))
Verification passed.

测试 toc 是否产生了负值,测试可持续 20 秒。即使测试由于历时不能为负数而失败,Eventually 也会在超时期限内持续轮询 toc

testCase.verifyThat(@toc,Eventually(IsLessThan(0)))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    Eventually failed.
    --> The constraint never passed with a timeout of 20 second(s).
    --> IsLessThan failed.
        --> The value must be less than the maximum value.
        
        Actual Value:
          45.073031200000003
        Maximum Value (Exclusive):
             0
    
    Evaluated Function:
      function_handle with value:
    
        @toc

调整时限,以便 Eventually 轮询 5 秒。如果在调用 ticverifyThat 之间未等待 5 秒以上,则测试将失败,原因是所用时间不超过 10 秒,在更改后的时限内。

tic
testCase.verifyThat(@toc,Eventually(IsGreaterThan(10), ...
    "WithTimeoutOf",5))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    Eventually failed.
    --> The constraint never passed with a timeout of 5 second(s).
    --> IsGreaterThan failed.
        --> The value must be greater than the minimum value.
        
        Actual Value:
           5.068235800000000
        Minimum Value (Exclusive):
            10
    
    Evaluated Function:
      function_handle with value:
    
        @toc

版本历史记录

在 R2013a 中推出