Clear Filters
Clear Filters

using asynchronous tests with Eventually in a testscript with runtests

6 views (last 30 days)
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 Comments
Lukas
Lukas on 3 Jan 2024
>> 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.

Sign in to comment.

Accepted Answer

Harsha Vardhan
Harsha Vardhan on 12 Jan 2024
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!

More Answers (0)

Tags

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!