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
The function assert_called_once() is a new addition to the python unittest.mock module. (see https://docs.python.org/3/library/unittest.mock.html). Unfortunately, a) if you use it against an older version of python it doesn't exist so it makes code not portable. b) if you use it anyway and then backport the code then you typically end up with a test case that passes but doesn't test anything which is nasty and dangerous.
The latter happens whenever a mock without a spec is used and the function is called against it.
In this case, the function call is mocked so it works. Note also that calls to function names with typos will also .
mock_object.assert_call_once_with(1) # WRONG - typo - this does nothing.
Personally I think it may be a good idea for pycodestyle to warn against any use of mock_object.assert* but it should definitely warn against likely typos.
The proper way to guard against this type of thing is proper TDD style writing of test cases where we ensure they can fail as well as pass. However even that doesn't work in the case of assert_called_once() if you develop against a new version of mock and then test on an old version
This warning might be less needed once all versions of python below 3.6 were obsolete.
The function assert_called_once() is a new addition to the python unittest.mock module. (see https://docs.python.org/3/library/unittest.mock.html). Unfortunately, a) if you use it against an older version of python it doesn't exist so it makes code not portable. b) if you use it anyway and then backport the code then you typically end up with a test case that passes but doesn't test anything which is nasty and dangerous.
The latter happens whenever a mock without a spec is used and the function is called against it.
mock_object.assert_called_once() # WRONG - doesn't exist in python < 3.6 / mock <
In this case, the function call is mocked so it works. Note also that calls to function names with typos will also .
mock_object.assert_call_once_with(1) # WRONG - typo - this does nothing.
Personally I think it may be a good idea for pycodestyle to warn against any use of mock_object.assert* but it should definitely warn against likely typos.
The proper way to guard against this type of thing is proper TDD style writing of test cases where we ensure they can fail as well as pass. However even that doesn't work in the case of assert_called_once() if you develop against a new version of mock and then test on an old version
This warning might be less needed once all versions of python below 3.6 were obsolete.
Edit: Links for further reference
The text was updated successfully, but these errors were encountered: