You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Provide assertion functions in unittest.mock #75132
The convenience assertion methods on mock objects can be easily mistyped and if they are mistyped, they will silently pass. This can be quite user-hostile. Consider the following:
>> example = Mock()
>> example.assert_called_once()
>> example.assert_caled_once_with(...)
This will not raise any exceptions, though the first feels natural and the latter is misspelt. To avoid using the methods, one can type:
but the meaning of that latter statement is particularly non-obvious. Instead, it would be great if I could import the assertions from mock as functions, and call them with mock as the first argument:
>> from unittest.mock import assert_called_once # This will be an ImportError
>> example = Mock()
>> assert_caled_once_with(example, ...) # A NameError
>> assert_called_once_with(example, ...) # Great success!
You can already do this, although it it somewhat of a hack:
>> assert_called_once=Mock.assert_called_once # This will be an AttributeError
>> assert_called_once_with=Mock.assert_called_once_with
>> example = Mock()
>> assert_caled_once_with(example, ...) # A NameError
>> assert_called_once_with(example, ...) # Great success!
Activity
OddBloke commentedon Jul 17, 2017
The convenience assertion methods on mock objects can be easily mistyped and if they are mistyped, they will silently pass. This can be quite user-hostile. Consider the following:
This will not raise any exceptions, though the first feels natural and the latter is misspelt. To avoid using the methods, one can type:
but the meaning of that latter statement is particularly non-obvious. Instead, it would be great if I could import the assertions from mock as functions, and call them with mock as the first argument:
pppery commentedon Jul 17, 2017
You can already do this, although it it somewhat of a hack:
berkerpeksag commentedon Oct 26, 2017
Thank you for the report. This is a duplicate of bpo-24651.