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;
  2 个评论
Lukas
Lukas 2024-1-3
>> myRuntTests
Running myTests
================================================================================
Error occurred in myTests/myTests and it did not run to completion.
---------
Error ID:
---------
'MATLAB:unittest:TestCase:InteractiveUseOnly'
--------------
Error Details:
--------------
Error using matlab.unittest.TestCase.forInteractiveUse
Unable to create an interactive TestCase while running a test.
Error in myTests (line 5)
testCase = TestCase.forInteractiveUse;
================================================================================
.
Done myTests
__________
Failure Summary:
Name Failed Incomplete Reason(s)
================================================
myTests/myTests X X Errored.
ans =
TestResult with properties:
Name: 'myTests/myTests'
Passed: 0
Failed: 1
Incomplete: 1
Duration: 0.009104
Details: [1×1 struct]
Totals:
0 Passed, 1 Failed (rerun), 1 Incomplete.
0.009104 seconds testing time.

请先登录,再进行评论。

采纳的回答

Harsha Vardhan
Harsha Vardhan 2024-1-12
Hi,
From what I gather, you would like to create a test script containing the Eventuallyclass 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:
  1. https://www.mathworks.com/help/matlab/ref/matlab.unittest.constraints.constraint-class.html
  2. 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 CenterFile Exchange 中查找有关 Write Unit Tests 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by