Merge "Make shared fixture class smarter to use."
This commit is contained in:
commit
cf647e1803
|
@ -27,8 +27,6 @@ get_fixture = fixture.get_fixture
|
||||||
get_fixture_name = fixture.get_fixture_name
|
get_fixture_name = fixture.get_fixture_name
|
||||||
remove_fixture = fixture.remove_fixture
|
remove_fixture = fixture.remove_fixture
|
||||||
setup_fixture = fixture.setup_fixture
|
setup_fixture = fixture.setup_fixture
|
||||||
setup_shared_fixture = fixture.setup_shared_fixture
|
|
||||||
cleanup_fixture = fixture.cleanup_fixture
|
cleanup_fixture = fixture.cleanup_fixture
|
||||||
cleanup_shared_fixture = fixture.cleanup_shared_fixture
|
|
||||||
list_required_fixtures = fixture.list_required_fixtures
|
list_required_fixtures = fixture.list_required_fixtures
|
||||||
SharedFixture = fixture.SharedFixture
|
SharedFixture = fixture.SharedFixture
|
||||||
|
|
|
@ -47,24 +47,12 @@ def setup_fixture(obj, manager=None):
|
||||||
return fixture
|
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):
|
def cleanup_fixture(obj, manager=None):
|
||||||
fixture = get_fixture(obj, manager=manager)
|
fixture = get_fixture(obj, manager=manager)
|
||||||
fixture.cleanUp()
|
fixture.cleanUp()
|
||||||
return fixture
|
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):
|
def iter_required_fixtures(objects):
|
||||||
objects = list(objects)
|
objects = list(objects)
|
||||||
while objects:
|
while objects:
|
||||||
|
@ -123,6 +111,9 @@ def get_object_name(obj):
|
||||||
if name:
|
if name:
|
||||||
return name
|
return name
|
||||||
|
|
||||||
|
if not inspect.isclass(obj):
|
||||||
|
obj = type(obj)
|
||||||
|
|
||||||
module = inspect.getmodule(obj).__name__
|
module = inspect.getmodule(obj).__name__
|
||||||
|
|
||||||
if six.PY2:
|
if six.PY2:
|
||||||
|
@ -205,24 +196,46 @@ class SharedFixture(fixtures.Fixture):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_setup_executed = False
|
_setup_executed = False
|
||||||
|
_cleanup_executed = False
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
# make sure class states can be used before setUp
|
||||||
self._clear_cleanups()
|
self._clear_cleanups()
|
||||||
|
|
||||||
def setUp(self):
|
def _remove_state(self):
|
||||||
"""Executes _setUp method only the first time setUp is called"""
|
# make sure class states can be used after cleanUp
|
||||||
if not self._setup_executed:
|
super(SharedFixture, self)._clear_cleanups()
|
||||||
self.setup_shared_fixture()
|
|
||||||
|
|
||||||
def setup_shared_fixture(self):
|
def setUp(self):
|
||||||
"""Forces execution of _setUp method"""
|
"""Executes _setUp/setup_fixture method only the first time is called
|
||||||
super(SharedFixture, self).setUp()
|
|
||||||
self._setup_executed = True
|
"""
|
||||||
|
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):
|
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):
|
def _setUp(self):
|
||||||
"""Executes registered cleanups"""
|
self.setup_fixture()
|
||||||
super(SharedFixture, self).cleanUp(raise_first)
|
|
||||||
self._setup_executed = False
|
@property
|
||||||
|
def fixture_name(self):
|
||||||
|
return get_fixture_name(self)
|
||||||
|
|
||||||
|
def setup_fixture(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def cleanup_fixture(self):
|
||||||
|
pass
|
||||||
|
|
|
@ -13,14 +13,14 @@
|
||||||
# under the License.
|
# under the License.
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
import fixtures
|
# import fixtures
|
||||||
import mock
|
import mock
|
||||||
|
|
||||||
import tobiko
|
import tobiko
|
||||||
from tobiko.tests import unit
|
from tobiko.tests import unit
|
||||||
|
|
||||||
|
|
||||||
class MyFixture(fixtures.Fixture):
|
class MyFixture(tobiko.SharedFixture):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@ -134,15 +134,17 @@ class SharedFixtureTest(unit.TobikoUnitTest):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(SharedFixtureTest, self).setUp()
|
super(SharedFixtureTest, self).setUp()
|
||||||
tobiko.remove_fixture(MySharedFixture)
|
tobiko.remove_fixture(MySharedFixture)
|
||||||
self.mock_setup = self.patch('fixtures.Fixture.setUp')
|
self.mock_setup = self.patch_object(
|
||||||
self.mock_cleanup = self.patch('fixtures.Fixture.cleanUp')
|
tobiko.SharedFixture, 'setup_fixture')
|
||||||
|
self.mock_cleanup = self.patch_object(
|
||||||
|
tobiko.SharedFixture, 'cleanup_fixture')
|
||||||
|
|
||||||
def test_initial_state(self):
|
def test_initial_state(self):
|
||||||
self.mock_setup.assert_not_called()
|
self.mock_setup.assert_not_called()
|
||||||
self.mock_cleanup.assert_not_called()
|
self.mock_cleanup.assert_not_called()
|
||||||
|
|
||||||
def test_use_fixture(self):
|
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)
|
fixture = tobiko.get_fixture(MySharedFixture)
|
||||||
|
|
||||||
self.useFixture(fixture)
|
self.useFixture(fixture)
|
||||||
|
@ -151,6 +153,12 @@ class SharedFixtureTest(unit.TobikoUnitTest):
|
||||||
self.useFixture(fixture)
|
self.useFixture(fixture)
|
||||||
self.mock_setup.assert_called_once_with()
|
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):
|
def test_setup_fixture(self):
|
||||||
tobiko.setup_fixture(MySharedFixture)
|
tobiko.setup_fixture(MySharedFixture)
|
||||||
tobiko.setup_fixture(MySharedFixture)
|
tobiko.setup_fixture(MySharedFixture)
|
||||||
|
@ -158,23 +166,14 @@ class SharedFixtureTest(unit.TobikoUnitTest):
|
||||||
|
|
||||||
def test_cleanup_fixture(self):
|
def test_cleanup_fixture(self):
|
||||||
tobiko.cleanup_fixture(MySharedFixture)
|
tobiko.cleanup_fixture(MySharedFixture)
|
||||||
self.mock_cleanup.assert_not_called()
|
self.mock_cleanup.assert_called_once_with()
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
def test_cleanup_shared_fixture_workflow(self):
|
def test_cleanup_shared_fixture_workflow(self):
|
||||||
tobiko.setup_fixture(MySharedFixture)
|
tobiko.setup_fixture(MySharedFixture)
|
||||||
tobiko.setup_fixture(MySharedFixture)
|
tobiko.setup_fixture(MySharedFixture)
|
||||||
self.mock_setup.assert_called_once_with()
|
self.mock_setup.assert_called_once_with()
|
||||||
|
|
||||||
tobiko.cleanup_shared_fixture(MySharedFixture)
|
tobiko.cleanup_fixture(MySharedFixture)
|
||||||
self.mock_cleanup.assert_called_once()
|
self.mock_cleanup.assert_called_once()
|
||||||
|
|
||||||
tobiko.setup_fixture(MySharedFixture)
|
tobiko.setup_fixture(MySharedFixture)
|
||||||
|
|
|
@ -28,12 +28,17 @@ class TobikoUnitTest(base.TobikoTest):
|
||||||
self.patch('oslo_log.log.setup')
|
self.patch('oslo_log.log.setup')
|
||||||
|
|
||||||
def patch(self, target, *args, **kwargs):
|
def patch(self, target, *args, **kwargs):
|
||||||
# pylint: disable=arguments-differ
|
|
||||||
context = mock.patch(target, *args, **kwargs)
|
context = mock.patch(target, *args, **kwargs)
|
||||||
mock_object = context.start()
|
mock_object = context.start()
|
||||||
self.addCleanup(context.stop)
|
self.addCleanup(context.stop)
|
||||||
return mock_object
|
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):
|
def create_temdir(self, *args, **kwargs):
|
||||||
dir_path = tempfile.mkdtemp(*args, **kwargs)
|
dir_path = tempfile.mkdtemp(*args, **kwargs)
|
||||||
self.addCleanup(shutil.rmtree(dir_path, ignore_errors=True))
|
self.addCleanup(shutil.rmtree(dir_path, ignore_errors=True))
|
||||||
|
|
Loading…
Reference in New Issue