From dc6147f933c44ae2d180bc55547f38382e499fdb Mon Sep 17 00:00:00 2001 From: Federico Ressi Date: Wed, 6 Mar 2019 11:07:18 +0100 Subject: [PATCH] Make shared fixture class smarter to use. Change-Id: I012ae957081bb34117315435b38556e553b88b7c --- tobiko/__init__.py | 2 - tobiko/common/managers/fixture.py | 63 +++++++++++++++++++------------ tobiko/tests/test_fixture.py | 31 ++++++++------- tobiko/tests/unit.py | 7 +++- 4 files changed, 59 insertions(+), 44 deletions(-) diff --git a/tobiko/__init__.py b/tobiko/__init__.py index 74c51f291..de6b5cfc4 100644 --- a/tobiko/__init__.py +++ b/tobiko/__init__.py @@ -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 diff --git a/tobiko/common/managers/fixture.py b/tobiko/common/managers/fixture.py index 4fcbabb01..466bbb30c 100644 --- a/tobiko/common/managers/fixture.py +++ b/tobiko/common/managers/fixture.py @@ -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 diff --git a/tobiko/tests/test_fixture.py b/tobiko/tests/test_fixture.py index bea6bcc99..b1e2d9f6b 100644 --- a/tobiko/tests/test_fixture.py +++ b/tobiko/tests/test_fixture.py @@ -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) diff --git a/tobiko/tests/unit.py b/tobiko/tests/unit.py index a9f5c82c9..05fcdf796 100644 --- a/tobiko/tests/unit.py +++ b/tobiko/tests/unit.py @@ -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))