[New-bugs-announce] [issue30821] unittest.mock.Mocks with specs aren't aware of default arguments

Max Rothman report at bugs.python.org
Fri Jun 30 13:13:29 EDT 2017


New submission from Max Rothman:

For a function f with the signature f(foo=None), the following three calls are equivalent:

f(None)
f(foo=None)
f()

However, only the first two are equivalent in the eyes of unittest.mock.Mock.assert_called_with:

>>> with patch('__main__.f', autospec=True) as f_mock:
        f_mock(foo=None)
        f_mock.assert_called_with(None)
<no exception>
>>> with patch('__main__.f', autospec=True) as f_mock:
        f_mock(None)
        f_mock.assert_called_with()
AssertionError: Expected call: f()  Actual call: f(None)

This is definitely surprising to new users (it was surprising to me!) and unnecessarily couples tests to how a particular piece of code happens to call a function.

----------
components: Library (Lib)
messages: 297433
nosy: Max Rothman
priority: normal
severity: normal
status: open
title: unittest.mock.Mocks with specs aren't aware of default arguments
versions: Python 2.7, Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30821>
_______________________________________


More information about the New-bugs-announce mailing list