Clear Filters
Clear Filters

VerifyEqual within a time interval

36 views (last 30 days)
Marco Montanaro
Marco Montanaro on 1 Aug 2024 at 8:26
Commented: Umar on 1 Aug 2024 at 17:40
Hello, I have been assigned a series of tests to be completed on several floating point variables. The assignment requires me to check whether the Expected value is the same as the Actual value. However, it may take a few milliseconds for the Actual value to reach the same status as the Expected value, and the verifyEqual function does not seem to consider this scenario.
I'd like to know if there is some sort of workaround for this situation. Basically, if A is the Actual value and E the Expected value, I'd like to modify (somehow) the verifyEqual function so that it returns a passing test if A == E with a time delay of approx. 3 milliseconds.
Thank you for your cooperation.
  5 Comments
Aquatris
Aquatris on 1 Aug 2024 at 12:32
But you did not answer the question but instead made a comment @Umar :P
Umar
Umar on 1 Aug 2024 at 17:40
@Aquatris, it’s weird that when I hit Answer this question button on my cell phone, it defaults to form and also layout of form is old version plus I am a poor guy who can’t afford all fancy things like Matlab toolbox. Just going through some hardships but my main goal is to make difference in people lives just like Jesus did. Sorry, for getting exaggerating too much.

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 1 Aug 2024 at 15:42
Yes, you can do this in the MATLAB unit testing framework. I assume the context is something along the lines of either A or E being a property of an object or the result of a function call. In that case, use verifyThat and the Eventually constraint to perform your testing using another constraint (in the case you described probably IsEqualTo, though for this example I'm going to use IsGreaterThan.) Give verifyThat a function handle it can evaluate repeatedly to poll whether or not the constraint you use inside Eventually is satisfied.
In this case, I want to check that after calling tic that the toc function will eventually return a value greater than 5, indicating that it's been 5 seconds. For this example that does happen after 5 seconds.
% Setup phase -- create the test object and import the constraints
testcase = matlab.unittest.TestCase.forInteractiveUse;
import matlab.unittest.constraints.Eventually
import matlab.unittest.constraints.IsGreaterThan
% Start the exercise phase
tic
% Verify that the toc function eventually returns a value greater than 5
verifyThat(testcase, @() toc, Eventually(IsGreaterThan(5)))
Verification passed.
toc % Should be a little over 5 seconds
Elapsed time is 5.143284 seconds.
% No teardown required
If I specify a timeout in the Eventually constraint and the condition isn't satisfied by the time that timeout is reached, the verifyThat verification will fail.
tic
verifyThat(testcase, @() toc, Eventually(IsGreaterThan(5), "WithTimeoutOf", 4))
Verification failed. --------------------- Framework Diagnostic: --------------------- Eventually failed. --> The constraint never passed with a timeout of 4 second(s). --> IsGreaterThan failed. --> The value must be greater than the minimum value. Actual Value: 4.009260000000000 Minimum Value (Exclusive): 5 Evaluated Function: function_handle with value: @()toc ------------------ Stack Information: ------------------ In /tmp/Editor_zhfgp/LiveEditorEvaluationHelperEeditorId.m (LiveEditorEvaluationHelperEeditorId) at 12 In /MATLAB/toolbox/matlab/connector2/interpreter/+connector/+internal/fevalMatlab.p (fevalMatlab) at 0 In /MATLAB/toolbox/matlab/connector2/interpreter/+connector/+internal/fevalJSON.p (fevalJSON) at 0
In this case, I wanted to see if toc said that more than 5 seconds had elapsed since the tic call, but I only waited for 4 seconds. The default timeout is 20 seconds which is why the first verifyThat test passed.

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!