-
-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow use of custom mock implementations #150
Comments
Please note: I rather suggest not touching If you really need |
I didn't take a look at the problem in detail, but @asvetlov makes a point about supporting slightly different APIs in the same package brings more problems than it helps. Perhaps we should create a new |
agree with your points on this. In terms of creating a new plugin, it might be better to try and get the fixtures into the The hack we have in place works well enough for us until that time, and for our specific usecase, it will be an easy migration. If anyone else stumbles on this issue, here is the hack: import pytest
import pytest_mock
import asynctest.mock
@pytest.fixture
def async_mocker(pytestconfig):
# This is a straight copy + paste from pytest_mock, but with our patched MockFixture
result = AsyncMockFixture(pytestconfig)
yield result
result.stopall()
class AsyncMockFixture(pytest_mock.MockFixture):
def __init__(self, config):
# This is a straight copy + paste from pytest_mock
# TODO: contribute a way to use arbitary mock libraries upstream
self._patches = [] # list of mock._patch objects
self._mocks = [] # list of MagicMock objects
# CHANGED: hard coding the asynctest.mock
self.mock_module = mock_module = asynctest.mock
self.patch = self._Patcher(self._patches, self._mocks, mock_module)
# aliases for convenience
self.Mock = mock_module.Mock
self.MagicMock = mock_module.MagicMock
self.NonCallableMock = mock_module.NonCallableMock
self.PropertyMock = mock_module.PropertyMock
self.call = mock_module.call
self.ANY = mock_module.ANY
self.DEFAULT = mock_module.DEFAULT
self.create_autospec = mock_module.create_autospec
self.sentinel = mock_module.sentinel
self.mock_open = mock_module.mock_open
# CoroutineMock is from asynctest
# AsyncMock is being added in python 3.8
# Please use AsyncMock.
self.CoroutineMock = self.AsyncMock = mock_module.CoroutineMock |
Python 3.8 is out. It might be worth reopening this issue as there's AsyncMock available now. |
Thanks for the ping @jack1142. Will gladly accept PRs in this regard if all it is required is to provide a custom mock implementation, but I probably would not like to include it into |
EDIT: Sorry I thought |
FWIW, |
With the growing use of
asyncio
, we need ways to mock Coroutines.Currently,
mock
andunittest.mock
do not support creating specs for coroutines (AsyncMock is being added in python 3.8).As a work around, we're using
asynctest.mock
, which has the same interface asunittest.mock
, however this requires us to completely re-implement the__init__
method ofMockFixture
, and removes the "hide traceback" functionality (though we don't care too much about this).This allows us to have both
mocker
andasync_mocker
fixtures.TLDR
To ease this usecase (and potentially others), Can support for defining a mock modules path from the config be added please?
The text was updated successfully, but these errors were encountered: