创建一个供交互测试的测试用例。
import matlab.unittest.TestCase
import matlab.unittest.constraints.Eventually
import matlab.unittest.constraints.IsGreaterThan
import matlab.unittest.constraints.IsLessThan
testCase = TestCase.forInteractiveUse;
验证在时限内调用 toc 得到的值大于 10(秒)。Eventually 约束重复调用 toc,直至满足约束或者所用时间超过时限。重复调用 toc 的所用时间自上次调用 tic 起计。
tic
testCase.verifyThat(@toc, Eventually(IsGreaterThan(10)))
Interactive verification passed.
验证过程可能需要长达 10 秒钟,toc 才能获得通过值。如果发出对 tic 的调用并等待 10 秒以上,然后再发出 verifyThat 命令,则验证过程将立即返回结果,因为 toc 已返回大于 10 的值。
确认在时限内 toc 未返回负值。
testCase.verifyThat(@toc, Eventually(IsLessThan(0)))
Interactive 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:
36.532254706346720
Maximum Value (Exclusive):
0
Evaluated Function:
function_handle with value:
@toc
由于已用时间不会小于零,因此可预期会失败。但是,Eventually 在整个超时期限内轮询 toc。
调整时限,以便 Eventually 轮询 5 秒。
tic
testCase.verifyThat(@toc, Eventually(IsGreaterThan(10), ...
'WithTimeoutOf', 5))
Interactive 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.143138452046230
Minimum Value (Exclusive):
10
Evaluated Function:
function_handle with value:
@toc
如果在调用 tic 和 verifyThat 之间未等待 5 秒以上,则测试将失败,原因是所用时间不超过 10 秒,在更改后的时限内。