Perform button press in test whilst waiting for user input

13 ビュー (過去 30 日間)
David Wilby
David Wilby 2021 年 12 月 22 日
コメント済み: David Wilby 2022 年 1 月 10 日
I have a method (let's call it wait_for_user_input_or_timeout) that waits for user input via a button in a uifigure, or reaches a timeout and exits the method.
The button callback basically just calls close(fig) to close the uifigure.
Whilst this works fine, I'm having trouble testing this, since in my test, executing wait_for_user_input_or_timeout blocks execution of the next statement until it completes. Whereas usually I would create a button, then use testCase.press() to mock user interaction, I can't do that here.
Does anyone have an idea of how to test this? I guess I should redesign my wait_for_user_input_or_timeout method to facilitate the testing.
P.S. I've tried running wait_for_user_input_or_timeout with parfeval or batch; parfeval unfortunately doesn't support figures, and batch makes a copy of the object, rather than working on the same object used in the code that calls it.

採用された回答

Sivani Pentapati
Sivani Pentapati 2022 年 1 月 7 日
Hi David,
Based on my understanding, you want to call testCase.press() when wait_for_user_input_or_timeout is being executed. There are two possible workarounds
  1. You can use waitfor function along with wait_for_user_input_or_timeout, where it would wait for the testCase.press() to be triggered before executing the main function.
  2. Alternatively, you can change the structure of the code where wait_for_user_input would be a standalone function which gets triggerd with a flag. You can switch the flag followed by executing the press event
Additionally as you have mentioned that you are using figures in your tests you can add custom interrupt callbacks to the figures if required. Please refer to the below link for more informaiton
  1 件のコメント
David Wilby
David Wilby 2022 年 1 月 10 日
Hi Sivani, thanks for your response. In the case of my code, I've done something along the lines of your second suggestion and restructured slightly to avoid blocking.
In my tests, what I've ended up doing is using timer to execute the testCase.press() action, i.e. starting the timer, then starting my wait_for_user_input_or_timeout function which then receives the button press.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSchedule Model Components についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by