Skip to content

Commit 6886887

Browse files
committed
Add support for assert_called()
This was added in Python 3.6. I also re-ordered the wrappers to match the order in the unittest.mock docs to make it easier to see if any are missing in the future. ref: https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.assert_called
1 parent 3dc96f9 commit 6886887

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
NEXT
2+
----
3+
4+
* Add support for the recently added ``assert_called`` method in Python 3.6 and ``mock-2.0``. Thanks `@rouge8`_ for the PR (`#115`_).
5+
6+
.. _#115: https://github.com/pytest-dev/pytest-mock/pull/115
7+
18
1.9.0
29
-----
310

pytest_mock.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ def wrap_assert_any_call(*args, **kwargs):
245245
*args, **kwargs)
246246

247247

248+
def wrap_assert_called(*args, **kwargs):
249+
__tracebackhide__ = True
250+
assert_wrapper(_mock_module_originals["assert_called"],
251+
*args, **kwargs)
252+
253+
248254
def wrap_assert_methods(config):
249255
"""
250256
Wrap assert methods of mock module so we can hide their traceback and
@@ -257,12 +263,13 @@ def wrap_assert_methods(config):
257263
mock_module = _get_mock_module(config)
258264

259265
wrappers = {
260-
'assert_not_called': wrap_assert_not_called,
266+
'assert_called': wrap_assert_called,
267+
'assert_called_once': wrap_assert_called_once,
261268
'assert_called_with': wrap_assert_called_with,
262269
'assert_called_once_with': wrap_assert_called_once_with,
263-
'assert_called_once': wrap_assert_called_once,
264-
'assert_has_calls': wrap_assert_has_calls,
265270
'assert_any_call': wrap_assert_any_call,
271+
'assert_has_calls': wrap_assert_has_calls,
272+
'assert_not_called': wrap_assert_not_called,
266273
}
267274
for method, wrapper in wrappers.items():
268275
try:

test_pytest_mock.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,18 @@ def test_assert_called_once_wrapper(mocker):
399399
stub.assert_called_once()
400400

401401

402+
def test_assert_called_wrapper(mocker):
403+
stub = mocker.stub()
404+
if not hasattr(stub, 'assert_called'):
405+
pytest.skip('assert_called_once not available')
406+
with assert_traceback():
407+
stub.assert_called()
408+
stub("foo")
409+
stub.assert_called()
410+
stub("foo")
411+
stub.assert_called()
412+
413+
402414
@pytest.mark.usefixtures('needs_assert_rewrite')
403415
def test_assert_called_args_with_introspection(mocker):
404416
stub = mocker.stub()

0 commit comments

Comments
 (0)