Skip to content

Commit 0797e3d

Browse files
Fix async assertion argument introspection (#611)
Async assertion introspection reads `call_args`, so a later call that was not awaited replaces the arguments shown for a failed await assertion. It can also add a misleading argument diff when the mock was never awaited. Use `await_args` for await assertions and retain `call_args` for synchronous assertions. Add regressions for positional and keyword arguments, never-awaited mocks, and synchronous assertions on `AsyncMock`. --------- Co-authored-by: Bruno Oliveira <[email protected]>
1 parent e24c334 commit 0797e3d

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

‎CHANGELOG.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Releases
44
Unreleased
55
----------
66

7+
* `#611 <https://github.com/pytest-dev/pytest-mock/pull/611>`_: Fixed async mock assertion introspection to use awaited arguments instead of the latest call's arguments.
8+
9+
710
* `#606 <https://github.com/pytest-dev/pytest-mock/pull/606>`_: ``mocker.resetall(return_value=True, side_effect=True)`` now also applies to non-callable mocks, such as those returned by ``mocker.create_autospec(SomeClass, instance=True)``. Previously both arguments were silently ignored for them.
811
* `#547 <https://github.com/pytest-dev/pytest-mock/issues/547>`_: Added ``SpyType`` for annotating ``mocker.spy`` results.
912
* Dropped support for EOL Python 3.9.

‎src/pytest_mock/plugin.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,13 @@ def assert_wrapper(
501501
else:
502502
__mock_self = args[0]
503503
msg = str(e)
504-
if __mock_self.call_args is not None:
505-
actual_args, actual_kwargs = __mock_self.call_args
504+
call_args = (
505+
__mock_self.await_args
506+
if "await" in __wrapped_mock_method__.__name__
507+
else __mock_self.call_args
508+
)
509+
if call_args is not None:
510+
actual_args, actual_kwargs = call_args
506511
introspection = ""
507512
try:
508513
assert actual_args == args[1:]

‎tests/test_pytest_mock.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,41 @@ def test_assert_has_calls(mocker: MockerFixture) -> None:
820820
stub.assert_has_calls([mocker.call("bar")])
821821

822822

823+
@pytest.mark.usefixtures("needs_assert_rewrite")
824+
@pytest.mark.parametrize(
825+
"assertion", ["assert_awaited_with", "assert_awaited_once_with", "assert_any_await"]
826+
)
827+
@pytest.mark.asyncio
828+
async def test_async_assertion_uses_await_arguments(
829+
mocker: MockerFixture, assertion: str
830+
) -> None:
831+
stub = mocker.AsyncMock()
832+
await stub("awaited", source="awaited")
833+
stub("called", source="called").close()
834+
835+
getattr(stub, assertion)("awaited", source="awaited")
836+
with pytest.raises(AssertionError) as exc_info:
837+
getattr(stub, assertion)("wrong", source="wrong")
838+
introspection = str(exc_info.value).split("pytest introspection follows:")[1]
839+
assert "'awaited'" in introspection
840+
assert "'called'" not in introspection
841+
842+
with pytest.raises(AssertionError) as exc_info:
843+
stub.assert_called_with("wrong", source="wrong")
844+
introspection = str(exc_info.value).split("pytest introspection follows:")[1]
845+
assert "'called'" in introspection
846+
assert "'awaited'" not in introspection
847+
848+
849+
@pytest.mark.usefixtures("needs_assert_rewrite")
850+
def test_async_assertion_without_await(mocker: MockerFixture) -> None:
851+
stub = mocker.AsyncMock()
852+
stub("called").close()
853+
with pytest.raises(AssertionError) as exc_info:
854+
stub.assert_awaited_with("expected")
855+
assert "pytest introspection follows:" not in str(exc_info.value)
856+
857+
823858
def test_assert_has_calls_multiple_calls(mocker: MockerFixture) -> None:
824859
stub = mocker.stub()
825860
stub("foo")

0 commit comments

Comments
 (0)