Create base mixin class for patching in unit tests from test cases and fixtures
Change-Id: Ie8a706809f975d2156601148267e4079666c0fbb
This commit is contained in:
parent
c8d2212bf3
commit
f493b9b1b5
|
@ -42,6 +42,7 @@ setup_fixture = _fixture.setup_fixture
|
|||
cleanup_fixture = _fixture.cleanup_fixture
|
||||
list_required_fixtures = _fixture.list_required_fixtures
|
||||
SharedFixture = _fixture.SharedFixture
|
||||
FixtureManager = _fixture.FixtureManager
|
||||
|
||||
load_object = loader_manager.load_object
|
||||
load_module = loader_manager.load_module
|
||||
|
|
|
@ -12,38 +12,11 @@
|
|||
# under the License.
|
||||
from __future__ import absolute_import
|
||||
|
||||
import shutil
|
||||
import tempfile
|
||||
|
||||
import mock
|
||||
from oslo_log import log
|
||||
|
||||
from tobiko.common import _fixture
|
||||
from tobiko.tests import base
|
||||
from tobiko.tests.unit import _case
|
||||
from tobiko.tests.unit import _patch
|
||||
|
||||
|
||||
class TobikoUnitTest(base.TobikoTest):
|
||||
TobikoUnitTest = _case.TobikoUnitTest
|
||||
|
||||
def setUp(self):
|
||||
super(TobikoUnitTest, self).setUp()
|
||||
# Protect from mis-configuring logging
|
||||
self.patch(log, 'setup')
|
||||
self.fixture_manager = manager = _fixture.FixtureManager()
|
||||
self.patch(_fixture, 'FIXTURES', manager)
|
||||
|
||||
def patch(self, obj, attribute, value=mock.DEFAULT, spec=None,
|
||||
create=False, spec_set=None, autospec=None,
|
||||
new_callable=None, **kwargs):
|
||||
# pylint: disable=arguments-differ
|
||||
context = mock.patch.object(target=obj, attribute=attribute, new=value,
|
||||
spec=spec, create=create,
|
||||
spec_set=spec_set, autospec=autospec,
|
||||
new_callable=new_callable, **kwargs)
|
||||
mocked = context.start()
|
||||
self.addCleanup(context.stop)
|
||||
return mocked
|
||||
|
||||
def create_tempdir(self, *args, **kwargs):
|
||||
dir_path = tempfile.mkdtemp(*args, **kwargs)
|
||||
self.addCleanup(shutil.rmtree(dir_path, ignore_errors=True))
|
||||
return dir_path
|
||||
PatchFixture = _patch.PatchFixture
|
||||
PatchMixin = _patch.PatchMixin
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
from __future__ import absolute_import
|
||||
|
||||
import inspect
|
||||
import shutil
|
||||
import tempfile
|
||||
|
||||
from oslo_log import log
|
||||
import testtools
|
||||
|
||||
import tobiko
|
||||
from tobiko.tests.unit import _patch
|
||||
|
||||
|
||||
class FixtureManagerPatch(tobiko.FixtureManager, _patch.PatchFixture):
|
||||
|
||||
def init_fixture(self, obj, name):
|
||||
fixture = super(FixtureManagerPatch, self).init_fixture(
|
||||
obj=obj, name=name)
|
||||
self.addCleanup(tobiko.cleanup_fixture, fixture)
|
||||
return fixture
|
||||
|
||||
def setup_fixture(self):
|
||||
self.patch(inspect.getmodule(tobiko.FixtureManager), 'FIXTURES',
|
||||
self)
|
||||
|
||||
|
||||
class TobikoUnitTest(_patch.PatchMixin, testtools.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TobikoUnitTest, self).setUp()
|
||||
# Protect from mis-configuring logging
|
||||
self.patch(log, 'setup')
|
||||
|
||||
# Make sure each unit test uses it's own fixture manager
|
||||
self.fixture_manager = manager = FixtureManagerPatch()
|
||||
self.useFixture(manager)
|
||||
|
||||
def create_tempdir(self, *args, **kwargs):
|
||||
dir_path = tempfile.mkdtemp(*args, **kwargs)
|
||||
self.addCleanup(shutil.rmtree(dir_path, ignore_errors=True))
|
||||
return dir_path
|
|
@ -0,0 +1,37 @@
|
|||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
from __future__ import absolute_import
|
||||
|
||||
import mock
|
||||
|
||||
import tobiko
|
||||
|
||||
|
||||
class PatchMixin(object):
|
||||
"""Mixin class with mock method helpers"""
|
||||
|
||||
def patch(self, obj, attribute, value=mock.DEFAULT, spec=None,
|
||||
create=False, spec_set=None, autospec=None,
|
||||
new_callable=None, **kwargs):
|
||||
# pylint: disable=arguments-differ
|
||||
context = mock.patch.object(target=obj, attribute=attribute, new=value,
|
||||
spec=spec, create=create,
|
||||
spec_set=spec_set, autospec=autospec,
|
||||
new_callable=new_callable, **kwargs)
|
||||
mocked = context.start()
|
||||
self.addCleanup(context.stop)
|
||||
return mocked
|
||||
|
||||
|
||||
class PatchFixture(PatchMixin, tobiko.SharedFixture):
|
||||
"""Fixture class with mock method helpers"""
|
Loading…
Reference in New Issue