2014-07-02 12:28:34 |
Pavlo Shchelokovskyy |
description |
Some unit tests suit use the following assertion on mock objects returned by mock.Mock / mock.MagicMock classes:
mocked.assert_called_once()
This is wrong, as the mock object has only methods assert_called_with() and assert_called_once_with() [1]. Any other method access not defined as mock interface will simply pass/return another mock.Mock object, so those assertions in unit tests are not actually checking anything as they do never raise any error.
The correct usage (if one cares only about call count and not arguments) is
self.assertEqual(1, mocked.call_count)
[1] http://www.voidspace.org.uk/python/mock/mock.html |
Some unit tests use the following assertion on mock objects returned by mock.Mock / mock.MagicMock classes:
mocked.assert_called_once()
This is wrong, as the mock object has only methods assert_called_with() and assert_called_once_with() [1]. Any other method access not defined as mock interface will simply pass/return another mock.Mock object, so those assertions in unit tests are not actually checking anything as they do never raise any error.
The correct usage (if one cares only about call count and not arguments) is
self.assertEqual(1, mocked.call_count)
[1] http://www.voidspace.org.uk/python/mock/mock.html |
|