Revert "Revert "Switch to unittest.mock from mock""

This reverts commit c743e68f72af45b24f783a271368aa07923c00d9.

Change-Id: Ia5a87ec7a65703692cba2edf8aa01202dab68173
This commit is contained in:
Ben Nemec 2020-04-03 14:23:47 +00:00
parent cdee551ae6
commit 21beb1537f
9 changed files with 31 additions and 25 deletions

View File

@ -15,7 +15,6 @@ keystoneauth1==3.4.0
linecache2==1.0.0 linecache2==1.0.0
MarkupSafe==1.0 MarkupSafe==1.0
mccabe==0.2.1 mccabe==0.2.1
mock==2.0.0
netaddr==0.7.18 netaddr==0.7.18
openstackdocstheme==1.18.1 openstackdocstheme==1.18.1
oslo.config==5.2.0 oslo.config==5.2.0

View File

@ -16,6 +16,7 @@
"""Common utilities used in testing""" """Common utilities used in testing"""
import logging import logging
from unittest import mock
import fixtures import fixtures
from oslotest import createfile from oslotest import createfile
@ -23,7 +24,6 @@ from oslotest import log
from oslotest import output from oslotest import output
from oslotest import timeout from oslotest import timeout
from six.moves import mock
import testtools import testtools
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)

View File

@ -15,9 +15,9 @@
# under the License. # under the License.
import functools import functools
from unittest import mock
import fixtures import fixtures
import mock
def _lazy_autospec_method(mocked_method, original_method, eat_self): def _lazy_autospec_method(mocked_method, original_method, eat_self):
@ -72,8 +72,10 @@ class _AutospecMockMixin(object):
original_attr = getattr(original_spec, name) original_attr = getattr(original_spec, name)
if callable(original_attr): if callable(original_attr):
# lazily autospec callable attribute. # lazily autospec callable attribute.
eat_self = mock.mock._must_skip(original_spec, name, eat_self = mock._must_skip(
isinstance(original_spec, type)) original_spec, name,
isinstance(original_spec, type)
)
_lazy_autospec_method(attr, original_attr, eat_self) _lazy_autospec_method(attr, original_attr, eat_self)
@ -110,15 +112,17 @@ class MockAutospecFixture(fixtures.Fixture):
super(MockAutospecFixture, self).setUp() super(MockAutospecFixture, self).setUp()
# patch both external and internal usage of Mock / MagicMock. # patch both external and internal usage of Mock / MagicMock.
self.useFixture(fixtures.MonkeyPatch('mock.Mock', _AutospecMock)) self.useFixture(
self.useFixture(fixtures.MonkeyPatch('mock.mock.Mock', _AutospecMock)) fixtures.MonkeyPatch(
self.useFixture(fixtures.MonkeyPatch('mock.MagicMock', 'unittest.mock.Mock',
_AutospecMagicMock)) _AutospecMock))
self.useFixture(fixtures.MonkeyPatch('mock.mock.MagicMock', self.useFixture(
_AutospecMagicMock)) fixtures.MonkeyPatch(
'unittest.mock.MagicMock',
_AutospecMagicMock))
class _patch(mock.mock._patch): class _patch(mock._patch):
"""Patch class with working autospec functionality. """Patch class with working autospec functionality.
Currently, mock.patch functionality doesn't handle the autospec parameter Currently, mock.patch functionality doesn't handle the autospec parameter
@ -159,8 +163,11 @@ class _patch(mock.mock._patch):
if autospec: if autospec:
target = self.getter() target = self.getter()
original_attr = getattr(target, self.attribute) original_attr = getattr(target, self.attribute)
eat_self = mock.mock._must_skip(target, self.attribute, eat_self = mock._must_skip(
isinstance(target, type)) target,
self.attribute,
isinstance(target, type)
)
new = super(_patch, self).__enter__() new = super(_patch, self).__enter__()
@ -186,11 +193,11 @@ def _safe_attribute_error_wrapper(func):
def patch_mock_module(): def patch_mock_module():
"""Replaces the mock.patch class.""" """Replaces the mock.patch class."""
mock.mock._patch = _patch mock._patch = _patch
# NOTE(claudiub): mock cannot autospec partial functions properly, # NOTE(claudiub): mock cannot autospec partial functions properly,
# especially those created by LazyLoader objects (scheduler client), # especially those created by LazyLoader objects (scheduler client),
# as it will try to copy the partial function's __name__ (which they do # as it will try to copy the partial function's __name__ (which they do
# not have). # not have).
mock.mock._copy_func_details = _safe_attribute_error_wrapper( mock._copy_func_details = _safe_attribute_error_wrapper(
mock.mock._copy_func_details) mock._copy_func_details)

View File

@ -17,10 +17,10 @@
import logging import logging
import os import os
import unittest import unittest
from unittest import mock
import fixtures import fixtures
import six import six
from six.moves import mock
import testtools import testtools
from oslotest import base from oslotest import base

View File

@ -15,8 +15,8 @@
# under the License. # under the License.
import logging import logging
from unittest import mock
from six.moves import mock
import testtools import testtools
from oslotest import log from oslotest import log

View File

@ -13,8 +13,8 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import mock
import testtools import testtools
from unittest import mock
from oslotest import mock_fixture from oslotest import mock_fixture

View File

@ -13,12 +13,12 @@
# under the License. # under the License.
import sys import sys
from unittest import mock
import testtools
from oslotest import output from oslotest import output
from six.moves import mock
import testtools
class CaptureOutputTest(testtools.TestCase): class CaptureOutputTest(testtools.TestCase):

View File

@ -12,7 +12,8 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from six.moves import mock from unittest import mock
import testtools import testtools
from oslotest import timeout from oslotest import timeout

View File

@ -6,4 +6,3 @@ fixtures>=3.0.0 # Apache-2.0/BSD
python-subunit>=1.0.0 # Apache-2.0/BSD python-subunit>=1.0.0 # Apache-2.0/BSD
six>=1.10.0 # MIT six>=1.10.0 # MIT
testtools>=2.2.0 # MIT testtools>=2.2.0 # MIT
mock>=2.0.0 # BSD