python / cpython

The Python programming language
https://www.python.org
Other
62.7k stars 30.06k forks source link

Mock.call_args and Mock.call_args_list not propagated to parent Mock #82686

Open 842fbbfe-d12e-45d0-a14a-2fb08ae92c65 opened 4 years ago

842fbbfe-d12e-45d0-a14a-2fb08ae92c65 commented 4 years ago
BPO 38505
Nosy @phmc, @tirkarthi

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = None closed_at = None created_at = labels = [] title = 'Mock.call_args and Mock.call_args_list not propagated to parent Mock' updated_at = user = 'https://bugs.python.org/CarisMoses' ``` bugs.python.org fields: ```python activity = actor = 'pconnell' assignee = 'none' closed = False closed_date = None closer = None components = [] creation = creator = 'Caris Moses' dependencies = [] files = [] hgrepos = [] issue_num = 38505 keywords = [] message_count = 1.0 messages = ['354841'] nosy_count = 3.0 nosy_names = ['pconnell', 'xtreak', 'Caris Moses'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = None url = 'https://bugs.python.org/issue38505' versions = [] ```

842fbbfe-d12e-45d0-a14a-2fb08ae92c65 commented 4 years ago

When I use some_mock.attach_mock(...) and make calls, the resulting some_mock.call_args is None while the some_mock.mock_calls list is not empty.

The code below shows this in Python 3.7.5:

from unittest import TestCase
from unittest.mock import patch, Mock

def foo(value):
    return value

class MyObjectTest(TestCase):
@patch(f'{__name__}.foo')
def test_do_something(self, mock_foo):
    manager = Mock()
    manager.attach_mock(mock_foo, 'foo_func')
    foo(3)
    print(manager.mock_calls)
    print(manager.call_args)
    print(manager.call_args_list)
if __name__ == "__main__":
    unittest.main()

The print statements return: [call.foo_func(3)] None []

While the code below (without attach_mock) works fine:

from unittest import TestCase
from unittest.mock import patch, Mock

def foo(value):
    return value

class MyObjectTest(TestCase):
@patch(f'{__name__}.foo')
def test_do_something(self, mock_foo):
    foo(3)
    print(mock_foo.mock_calls)
    print(mock_foo.call_args)
    print(mock_foo.call_args_list)
if __name__ == "__main__":
    unittest.main()

Print statements correctly return: [call(3)] call(3) [call(3)]

I also tested in Python 3.8.0 and got the same result.