Skip to content

Commit

Permalink
Merge pull request #242 from The-Compiler/assert-called
Browse files Browse the repository at this point in the history
Add CallbackBlocker.assert_called_with()
  • Loading branch information
nicoddemus committed Sep 26, 2018
2 parents 6194a02 + 02140ef commit 3392573
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
3.2.0 (unreleased)
------------------

- The ``CallbackBlocker`` returned by ``qtbot.waitCallback()`` now has a new
``assert_called_with(...)`` convenience method.

3.1.0 (2018-09-23)
------------------

Expand Down
7 changes: 7 additions & 0 deletions docs/wait_callback.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,10 @@ In the example above, we could check the result via:
assert cb.args == [2]
assert cb.kwargs == {}
Instead of checking the arguments by hand, you can use ``.assert_called_with()``
to make sure the callback was called with the given arguments:

.. code-block:: python
cb.assert_called_with(2)
9 changes: 9 additions & 0 deletions pytestqt/wait_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,15 @@ def wait(self):
if not self.called and self.raising:
raise TimeoutError("Callback wasn't called after %sms." % self.timeout)

def assert_called_with(self, *args, **kwargs):
"""
Check that the callback was called with the same arguments as this
function.
"""
assert self.called
assert self.args == list(args)
assert self.kwargs == kwargs

def _quit_loop_by_timeout(self):
try:
self._cleanup()
Expand Down
12 changes: 12 additions & 0 deletions tests/test_wait_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,18 @@ def test_args(self, qtbot):
assert callback.args == [23]
assert callback.kwargs == {"answer": 42}

def test_assert_called_with(self, qtbot):
with qtbot.waitCallback() as callback:
callback(23, answer=42)
callback.assert_called_with(23, answer=42)

def test_assert_called_with_wrong(self, qtbot):
with qtbot.waitCallback() as callback:
callback(23, answer=42)

with pytest.raises(AssertionError):
callback.assert_called_with(23)

def test_explicit(self, qtbot):
blocker = qtbot.waitCallback()
assert not blocker.called
Expand Down

0 comments on commit 3392573

Please sign in to comment.