VerifyEqual within a time interval

5 次查看(过去 30 天)
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 个评论
Aquatris
Aquatris 2024-8-1
But you did not answer the question but instead made a comment @Umar :P
Umar
Umar 2024-8-1
@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.

请先登录,再进行评论。

回答(1 个)

Steven Lord
Steven Lord 2024-8-1
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.

类别

Help CenterFile Exchange 中查找有关 Testing Frameworks 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by