using asynchronous tests with Eventually in a testscript with runtests
4 次查看(过去 30 天)
显示 更早的评论
The Eventually-method is great for testing time-related checks without having to wait a fixed sufficiently-high amount of time, but how do I get it working in a testscript with runtests.m, without rewriting my tests from a test-script to a test-class?
Minimum working example:
myTests.m:
import matlab.unittest.TestCase
import matlab.unittest.constraints.Eventually
import matlab.unittest.constraints.IsGreaterThan
testCase = TestCase.forInteractiveUse;
tic
testCase.verifyThat(@toc,Eventually(IsGreaterThan(1)))
When I run the tests as below, it crashes
runtests("myTests.m")
with the following short error (full error further down in comment):
Error using matlab.unittest.TestCase.forInteractiveUse
Unable to create an interactive TestCase while running a test.
Error in myTests (line 7)
testCase = TestCase.forInteractiveUse;
采纳的回答
Harsha Vardhan
2024-1-12
Hi,
From what I gather, you would like to create a test script containing the ‘Eventually’ class and execute it with the ‘runtests’ function. This resulted in an error.
The problem here stems from the use of 'TestCase.forInteractiveUse’ in a script intended for non-interactive execution with ‘runtests’. ‘TestCase.forInteractiveUse’ is specifically designed for interactive debugging sessions like in a Command Window, which is why it's causing an error in your automated test environment.
For more details on ‘TestCase.forInteractiveUse’, you can refer to MATLAB's documentation here: https://www.mathworks.com/help/matlab/ref/matlab.unittest.testcase.forinteractiveuse.html .
This error can be resolved by avoiding 'TestCase.forInteractiveUse’.You may use the ‘satisfiedBy’ method of the ‘Eventually’ class and the ‘assert’ function instead. Kindly refer to the following documentation to know more about the ‘satisfiedBy’ method of the constraint class and the ‘assert’ function:
- https://www.mathworks.com/help/matlab/ref/matlab.unittest.constraints.constraint-class.html
- https://www.mathworks.com/help/sltest/ref/assert.html
Please check the modified code of ‘myTests.m’ below:
import matlab.unittest.constraints.Eventually
import matlab.unittest.constraints.IsGreaterThan
tic
assert(Eventually(IsGreaterThan(19)).satisfiedBy(@toc))
I hope my response has been helpful to you!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Write Unit Tests 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!