-
-
Notifications
You must be signed in to change notification settings - Fork 70
Add waitUntil method to QtBot #139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ pytest-qt | |
| tutorial | ||
| logging | ||
| signals | ||
| wait_until | ||
| virtual_methods | ||
| modeltester | ||
| app_exit | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| waitUntil: Waiting for arbitrary conditions | ||
| =========================================== | ||
|
|
||
| .. versionadded:: 2.0 | ||
|
|
||
| Sometimes your tests need to wait a certain condition which does not trigger a signal, for example | ||
| that a certain control gained focus or a ``QListView`` has been populated with all items. | ||
|
|
||
| For those situations you can use :meth:`qtbot.waitUntil <pytestqt.plugin.QtBot.waitUntil>` to | ||
| wait until a certain condition has been met or a timeout is reached. This is specially important | ||
| in X window systems due to their asynchronous nature, where you can't rely on the fact that the | ||
| result of an action will be immediately available. | ||
|
|
||
| For example: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| def test_validate(qtbot): | ||
| window = MyWindow() | ||
| window.edit.setText('not a number') | ||
| # after focusing, should update status label | ||
| window.edit.setFocus() | ||
| assert window.status.text() == 'Please input a number' | ||
|
|
||
|
|
||
| The ``window.edit.setFocus()`` may not be processed immediately, only in a future event loop, which | ||
| might lead to this test to work sometimes and fail in others (a *flaky* test). | ||
|
|
||
| A better approach in situations like this is to use ``qtbot.waitUntil`` with a callback with your | ||
| assertion: | ||
|
|
||
|
|
||
| .. code-block:: python | ||
|
|
||
| def test_validate(qtbot): | ||
| window = MyWindow() | ||
| window.edit.setText('not a number') | ||
| # after focusing, should update status label | ||
| window.edit.setFocus() | ||
| def check_label(): | ||
| assert window.status.text() == 'Please input a number' | ||
| qtbot.waitUntil(check_label) | ||
|
|
||
|
|
||
| ``qtbot.waitUntil`` will periodically call ``check_label`` until it no longer raises | ||
| ``AssertionError`` or a timeout is reached. If a timeout is reached, the last assertion error | ||
| re-raised and the test will fail: | ||
|
|
||
| :: | ||
|
|
||
| _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | ||
| def check_label(): | ||
| > assert window.status.text() == 'Please input a number' | ||
| E assert 'OK' == 'Please input a number' | ||
| E - OK | ||
| E + Please input a number | ||
|
|
||
|
|
||
| A second way to use ``qtbot.waitUntil`` is to pass a callback which returns ``True`` when the | ||
| condition is met or ``False`` otherwise. It is usually terser than using a separate callback with | ||
| ``assert`` statement, but it produces a generic message when it fails because it can't make | ||
| use of ``pytest``'s assertion rewriting: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| def test_validate(qtbot): | ||
| window = MyWindow() | ||
| window.edit.setText('not a number') | ||
| # after focusing, should update status label | ||
| window.edit.setFocus() | ||
| qtbot.waitUntil(lambda: window.edit.hasFocus()) | ||
| assert window.status.text() == 'Please input a number' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import pytest | ||
|
|
||
|
|
||
| def test_wait_until(qtbot, wait_4_ticks_callback, tick_counter): | ||
| tick_counter.start(100) | ||
| qtbot.waitUntil(wait_4_ticks_callback, 1000) | ||
| assert tick_counter.ticks >= 4 | ||
|
|
||
|
|
||
| def test_wait_until_timeout(qtbot, wait_4_ticks_callback, tick_counter): | ||
| tick_counter.start(200) | ||
| with pytest.raises(AssertionError): | ||
| qtbot.waitUntil(wait_4_ticks_callback, 100) | ||
| assert tick_counter.ticks < 4 | ||
|
|
||
|
|
||
| def test_invalid_callback_return_value(qtbot): | ||
| with pytest.raises(ValueError): | ||
| qtbot.waitUntil(lambda: []) | ||
|
|
||
|
|
||
| def test_pep8_alias(qtbot): | ||
| qtbot.wait_until | ||
|
|
||
|
|
||
| @pytest.fixture(params=['predicate', 'assert']) | ||
| def wait_4_ticks_callback(request, tick_counter): | ||
| """Parametrized fixture which returns the two possible callback methods that can be | ||
| passed to ``waitUntil``: predicate and assertion. | ||
| """ | ||
| if request.param == 'predicate': | ||
| return lambda: tick_counter.ticks >= 4 | ||
| else: | ||
| def check_ticks(): | ||
| assert tick_counter.ticks >= 4 | ||
| return check_ticks | ||
|
|
||
|
|
||
| @pytest.yield_fixture | ||
| def tick_counter(): | ||
| """ | ||
| Returns an object which counts timer "ticks" periodically. | ||
| """ | ||
| from pytestqt.qt_compat import QtCore | ||
|
|
||
| class Counter: | ||
|
|
||
| def __init__(self): | ||
| self._ticks = 0 | ||
| self.timer = QtCore.QTimer() | ||
| self.timer.timeout.connect(self._tick) | ||
|
|
||
| def start(self, ms): | ||
| self.timer.start(ms) | ||
|
|
||
| def _tick(self): | ||
| self._ticks += 1 | ||
|
|
||
| @property | ||
| def ticks(self): | ||
| return self._ticks | ||
|
|
||
| counter = Counter() | ||
| yield counter | ||
| counter.timer.stop() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this should be an argument?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thought about it, but didn't think it would end up being necessary... perhaps we should wait until someone asks for it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like a plan 😉