Pytest works with old mock, but not unittest.mock
Yes, mock decorators are not supported.
It's not such bad -- changing function signature by decorator appliance is considered as bad idea.
But you still may use with mock.patch(...)
syntax.
Also as an option there is pytest-mock plugin with pretty clean api for mocking:
def test_foo(mocker):
# all valid calls
mocker.patch('os.remove')
mocker.patch.object(os, 'listdir', autospec=True)
mocked_isfile = mocker.patch('os.path.isfile')
There was a pytest
issue that now seems to be solved in a newer version of pytest
:
https://github.com/pytest-dev/pytest/pull/3206/commits/b6166dccb4d2b48173aa7e7739be52db9d2d56a0
Basically, if you had mock
installed, this would fail. You can verify by uninstalling mock
and running the test again.
If you really need that version of pytest, you can get the mock using with patch(..)
inside the function.