Implement get method on SharedFixture class to shorcut get_fixture method

Change-Id: I385de14b4d74a36dcd766be4a5ee46d07ad18e26
This commit is contained in:
Federico Ressi 2019-03-07 11:38:28 +01:00
parent 3ad5cce3c9
commit 13947823b6
2 changed files with 17 additions and 0 deletions

View File

@ -16,10 +16,13 @@ from __future__ import absolute_import
import inspect
import fixtures
from oslo_log import log
import six
import tobiko
LOG = log.getLogger(__name__)
def is_fixture(obj):
return (getattr(obj, '__tobiko_fixture__', False) or
@ -43,12 +46,14 @@ def remove_fixture(obj, manager=None):
def setup_fixture(obj, manager=None):
fixture = get_fixture(obj, manager=manager)
LOG.info('Set up fixture %r', fixture.__tobiko_fixture_name__)
fixture.setUp()
return fixture
def cleanup_fixture(obj, manager=None):
fixture = get_fixture(obj, manager=manager)
LOG.info('Clean up fixture %r', fixture.__tobiko_fixture_name__)
fixture.cleanUp()
return fixture
@ -202,6 +207,10 @@ class SharedFixture(fixtures.Fixture):
# make sure class states can be used before setUp
self._clear_cleanups()
@classmethod
def get(cls, manager=None):
return get_fixture(cls, manager=manager)
def _remove_state(self):
# make sure class states can be used after cleanUp
super(SharedFixture, self)._clear_cleanups()

View File

@ -122,11 +122,19 @@ class FixtureManagerTest(unit.TobikoUnitTest):
class SharedFixtureTest(unit.TobikoUnitTest):
def setUp(self):
super(SharedFixtureTest, self).setUp()
tobiko.remove_fixture(MyFixture)
def test_init(self):
fixture = MyFixture()
fixture.setup_fixture.assert_not_called()
fixture.cleanup_fixture.assert_not_called()
def test_get(self):
fixture = MyFixture.get()
self.assertIs(tobiko.get_fixture(MyFixture), fixture)
def test_use_fixture(self):
fixture = MyFixture()
self.addCleanup(fixture.cleanup_fixture.assert_called_once_with)