Skip to content
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

Add support for assert_called() #115

Merged
merged 1 commit into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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