using asynchronous tests with Eventually in a testscript with runtests
조회 수: 2 (최근 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!