Skip to content

Commit

Permalink
Merge pull request #115 from rouge8/feature/assert-called
Browse files Browse the repository at this point in the history
Add support for `assert_called()`
  • Loading branch information
nicoddemus committed Apr 11, 2018
2 parents 3dc96f9 + 6886887 commit 887fec3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
NEXT
----

* Add support for the recently added ``assert_called`` method in Python 3.6 and ``mock-2.0``. Thanks `@rouge8`_ for the PR (`#115`_).

.. _#115: https://github.com/pytest-dev/pytest-mock/pull/115

1.9.0
-----

Expand Down
13 changes: 10 additions & 3 deletions pytest_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ def wrap_assert_any_call(*args, **kwargs):
*args, **kwargs)


def wrap_assert_called(*args, **kwargs):
__tracebackhide__ = True
assert_wrapper(_mock_module_originals["assert_called"],
*args, **kwargs)


def wrap_assert_methods(config):
"""
Wrap assert methods of mock module so we can hide their traceback and
Expand All @@ -257,12 +263,13 @@ def wrap_assert_methods(config):
mock_module = _get_mock_module(config)

wrappers = {
'assert_not_called': wrap_assert_not_called,
'assert_called': wrap_assert_called,
'assert_called_once': wrap_assert_called_once,
'assert_called_with': wrap_assert_called_with,
'assert_called_once_with': wrap_assert_called_once_with,
'assert_called_once': wrap_assert_called_once,
'assert_has_calls': wrap_assert_has_calls,
'assert_any_call': wrap_assert_any_call,
'assert_has_calls': wrap_assert_has_calls,
'assert_not_called': wrap_assert_not_called,
}
for method, wrapper in wrappers.items():
try:
Expand Down
12 changes: 12 additions & 0 deletions test_pytest_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,18 @@ def test_assert_called_once_wrapper(mocker):
stub.assert_called_once()


def test_assert_called_wrapper(mocker):
stub = mocker.stub()
if not hasattr(stub, 'assert_called'):
pytest.skip('assert_called_once not available')
with assert_traceback():
stub.assert_called()
stub("foo")
stub.assert_called()
stub("foo")
stub.assert_called()


@pytest.mark.usefixtures('needs_assert_rewrite')
def test_assert_called_args_with_introspection(mocker):
stub = mocker.stub()
Expand Down

0 comments on commit 887fec3

Please sign in to comment.