Skip to content

Commit 9ac9bd7

Browse files
committed
Add support for assert_called_once()
This was added in Python 3.6. ref: https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.assert_called_once
1 parent 4bd938a commit 9ac9bd7

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

pytest_mock.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ def wrap_assert_called_with(*args, **kwargs):
221221
*args, **kwargs)
222222

223223

224+
def wrap_assert_called_once(*args, **kwargs):
225+
__tracebackhide__ = True
226+
assert_wrapper(_mock_module_originals["assert_called_once"],
227+
*args, **kwargs)
228+
229+
224230
def wrap_assert_called_once_with(*args, **kwargs):
225231
__tracebackhide__ = True
226232
assert_wrapper(_mock_module_originals["assert_called_once_with"],
@@ -254,6 +260,7 @@ def wrap_assert_methods(config):
254260
'assert_not_called': wrap_assert_not_called,
255261
'assert_called_with': wrap_assert_called_with,
256262
'assert_called_once_with': wrap_assert_called_once_with,
263+
'assert_called_once': wrap_assert_called_once,
257264
'assert_has_calls': wrap_assert_has_calls,
258265
'assert_any_call': wrap_assert_any_call,
259266
}

test_pytest_mock.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,17 @@ def test_assert_called_once_with_wrapper(mocker):
388388
stub.assert_called_once_with("foo")
389389

390390

391+
def test_assert_called_once_wrapper(mocker):
392+
stub = mocker.stub()
393+
if not hasattr(stub, 'assert_called_once'):
394+
pytest.skip('assert_called_once not available')
395+
stub("foo")
396+
stub.assert_called_once()
397+
stub("foo")
398+
with assert_traceback():
399+
stub.assert_called_once()
400+
401+
391402
@pytest.mark.usefixtures('needs_assert_rewrite')
392403
def test_assert_called_args_with_introspection(mocker):
393404
stub = mocker.stub()

0 commit comments

Comments
 (0)