Make shared fixture class smarter to use.

Change-Id: I012ae957081bb34117315435b38556e553b88b7c
This commit is contained in:
Federico Ressi 2019-03-06 11:07:18 +01:00
parent e0cfad7d80
commit dc6147f933
4 changed files with 59 additions and 44 deletions

View File

@ -27,8 +27,6 @@ get_fixture = fixture.get_fixture
get_fixture_name = fixture.get_fixture_name
remove_fixture = fixture.remove_fixture
setup_fixture = fixture.setup_fixture
setup_shared_fixture = fixture.setup_shared_fixture
cleanup_fixture = fixture.cleanup_fixture
cleanup_shared_fixture = fixture.cleanup_shared_fixture
list_required_fixtures = fixture.list_required_fixtures
SharedFixture = fixture.SharedFixture

View File

@ -47,24 +47,12 @@ def setup_fixture(obj, manager=None):
return fixture
def setup_shared_fixture(obj, manager=None):
fixture = get_fixture(obj, manager=manager)
fixture.setup_shared_fixture()
return fixture
def cleanup_fixture(obj, manager=None):
fixture = get_fixture(obj, manager=manager)
fixture.cleanUp()
return fixture
def cleanup_shared_fixture(obj, manager=None):
fixture = get_fixture(obj, manager=manager)
fixture.cleanup_shared_fixture()
return fixture
def iter_required_fixtures(objects):
objects = list(objects)
while objects:
@ -123,6 +111,9 @@ def get_object_name(obj):
if name:
return name
if not inspect.isclass(obj):
obj = type(obj)
module = inspect.getmodule(obj).__name__
if six.PY2:
@ -205,24 +196,46 @@ class SharedFixture(fixtures.Fixture):
"""
_setup_executed = False
_cleanup_executed = False
def __init__(self):
# make sure class states can be used before setUp
self._clear_cleanups()
def setUp(self):
"""Executes _setUp method only the first time setUp is called"""
if not self._setup_executed:
self.setup_shared_fixture()
def _remove_state(self):
# make sure class states can be used after cleanUp
super(SharedFixture, self)._clear_cleanups()
def setup_shared_fixture(self):
"""Forces execution of _setUp method"""
super(SharedFixture, self).setUp()
self._setup_executed = True
def setUp(self):
"""Executes _setUp/setup_fixture method only the first time is called
"""
if not self._setup_executed:
try:
super(SharedFixture, self).setUp()
finally:
self._cleanup_executed = False
self._setup_executed = True
def cleanUp(self, raise_first=True):
"""Id doesn't nothing"""
"""Executes registered cleanups if any"""
try:
if not self._cleanup_executed:
self.addCleanup(self.cleanup_fixture)
super(SharedFixture, self).cleanUp(raise_first=raise_first)
finally:
self._setup_executed = False
self._cleanup_executed = True
def cleanup_shared_fixture(self, raise_first=True):
"""Executes registered cleanups"""
super(SharedFixture, self).cleanUp(raise_first)
self._setup_executed = False
def _setUp(self):
self.setup_fixture()
@property
def fixture_name(self):
return get_fixture_name(self)
def setup_fixture(self):
pass
def cleanup_fixture(self):
pass

View File

@ -13,14 +13,14 @@
# under the License.
from __future__ import absolute_import
import fixtures
# import fixtures
import mock
import tobiko
from tobiko.tests import unit
class MyFixture(fixtures.Fixture):
class MyFixture(tobiko.SharedFixture):
pass
@ -134,15 +134,17 @@ class SharedFixtureTest(unit.TobikoUnitTest):
def setUp(self):
super(SharedFixtureTest, self).setUp()
tobiko.remove_fixture(MySharedFixture)
self.mock_setup = self.patch('fixtures.Fixture.setUp')
self.mock_cleanup = self.patch('fixtures.Fixture.cleanUp')
self.mock_setup = self.patch_object(
tobiko.SharedFixture, 'setup_fixture')
self.mock_cleanup = self.patch_object(
tobiko.SharedFixture, 'cleanup_fixture')
def test_initial_state(self):
self.mock_setup.assert_not_called()
self.mock_cleanup.assert_not_called()
def test_use_fixture(self):
self.addCleanup(self.mock_cleanup.assert_not_called)
self.addCleanup(self.mock_cleanup.assert_called_once_with)
fixture = tobiko.get_fixture(MySharedFixture)
self.useFixture(fixture)
@ -151,6 +153,12 @@ class SharedFixtureTest(unit.TobikoUnitTest):
self.useFixture(fixture)
self.mock_setup.assert_called_once_with()
def test_add_cleanup(self):
self.addCleanup(self.mock_cleanup.assert_called_once_with)
fixture = tobiko.get_fixture(MySharedFixture)
self.addCleanup(fixture.cleanUp)
self.addCleanup(fixture.cleanUp)
def test_setup_fixture(self):
tobiko.setup_fixture(MySharedFixture)
tobiko.setup_fixture(MySharedFixture)
@ -158,23 +166,14 @@ class SharedFixtureTest(unit.TobikoUnitTest):
def test_cleanup_fixture(self):
tobiko.cleanup_fixture(MySharedFixture)
self.mock_cleanup.assert_not_called()
def test_setup_shared_fixture(self):
tobiko.setup_shared_fixture(MySharedFixture)
tobiko.setup_shared_fixture(MySharedFixture)
self.mock_setup.assert_has_calls([mock.call(), mock.call()])
def test_cleanup_shared_fixture(self):
tobiko.cleanup_shared_fixture(MySharedFixture)
self.mock_cleanup.assert_called_once()
self.mock_cleanup.assert_called_once_with()
def test_cleanup_shared_fixture_workflow(self):
tobiko.setup_fixture(MySharedFixture)
tobiko.setup_fixture(MySharedFixture)
self.mock_setup.assert_called_once_with()
tobiko.cleanup_shared_fixture(MySharedFixture)
tobiko.cleanup_fixture(MySharedFixture)
self.mock_cleanup.assert_called_once()
tobiko.setup_fixture(MySharedFixture)

View File

@ -28,12 +28,17 @@ class TobikoUnitTest(base.TobikoTest):
self.patch('oslo_log.log.setup')
def patch(self, target, *args, **kwargs):
# pylint: disable=arguments-differ
context = mock.patch(target, *args, **kwargs)
mock_object = context.start()
self.addCleanup(context.stop)
return mock_object
def patch_object(self, target, attribute, *args, **kwargs):
context = mock.patch.object(target, attribute, *args, **kwargs)
mock_object = context.start()
self.addCleanup(context.stop)
return mock_object
def create_temdir(self, *args, **kwargs):
dir_path = tempfile.mkdtemp(*args, **kwargs)
self.addCleanup(shutil.rmtree(dir_path, ignore_errors=True))