Skip to content

Misleading introspection for assert_called_once, assert_any_call and similar assertions #640

Closed

Description

@nicoddemus

pytest-mock adds introspection to failed mock assertions by comparing the mock's last call with the arguments given to the assertion. That doesn't fit two groups of assertions:

Assertions without arguments: assert_called_once, assert_not_called, assert_called, assert_awaited_once, assert_not_awaited, assert_awaited. The last call gets compared with the empty arguments of the assertion, which suggests an argument mismatch when the problem is the call count:

def test_called_once(mocker):
    m = mocker.stub()
    m(1); m(2)
    m.assert_called_once()
AssertionError: Expected 'mock' to have been called once. Called 2 times.
Calls: [call(1), call(2)].

pytest introspection follows:

Args:
assert (2,) == ()

assert_any_call / assert_any_await: these search all calls, but the introspection only compares the last one:

def test_any_call(mocker):
    m = mocker.stub()
    m(1); m(2); m(3)
    m.assert_any_call(4)
AssertionError: mock(4) call not found

pytest introspection follows:

Args:
assert (3,) == (4,)

Both reproduced on 3.16.1. Probably the simplest fix is to skip the introspection for these assertions and keep only the traceback hiding: no single call is "the" expected one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions