using asynchronous tests with Eventually in a testscript with runtests
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Lukas
am 3 Jan. 2024
Kommentiert: Lukas
am 15 Jan. 2024
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;
2 Kommentare
Akzeptierte Antwort
Harsha Vardhan
am 12 Jan. 2024
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!
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Write Unit Tests finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!