get_fixture with fixture instance return instance itself.

Change-Id: Ie1528b941316579f807aa91aac28ed514b1e3a9c
changes/20/647420/5
Federico Ressi 4 years ago
parent f53d5bb512
commit b907b1edd2

@ -25,6 +25,8 @@ discover_testcases = testcase_manager.discover_testcases
is_fixture = fixture.is_fixture
get_fixture = fixture.get_fixture
get_fixture_name = fixture.get_fixture_name
get_fixture_class = fixture.get_fixture_class
get_fixture_dir = fixture.get_fixture_dir
remove_fixture = fixture.remove_fixture
setup_fixture = fixture.setup_fixture
cleanup_fixture = fixture.cleanup_fixture

@ -13,6 +13,7 @@
# under the License.
from __future__ import absolute_import
import os
import inspect
import fixtures
@ -31,12 +32,37 @@ def is_fixture(obj):
def get_fixture(obj, manager=None):
manager = manager or FIXTURES
return manager.get_fixture(obj)
if isinstance(obj, fixtures.Fixture):
return obj
else:
manager = manager or FIXTURES
return manager.get_fixture(obj)
def get_fixture_name(obj):
return get_fixture(obj).__tobiko_fixture_name__
try:
return obj.__tobiko_fixture_name__
except AttributeError:
name = get_object_name(obj)
if is_fixture(obj):
obj.__tobiko_fixture__ = True
obj.__tobiko_fixture_name__ = name
return name
def get_fixture_class(obj):
if isinstance(obj, six.string_types):
obj = tobiko.load_object(obj)
if not inspect.isclass(obj):
obj = type(obj)
assert issubclass(obj, fixtures.Fixture)
return obj
def get_fixture_dir(obj):
return os.path.dirname(inspect.getfile(get_fixture_class(obj)))
def remove_fixture(obj, manager=None):
@ -46,14 +72,14 @@ def remove_fixture(obj, manager=None):
def setup_fixture(obj, manager=None):
fixture = get_fixture(obj, manager=manager)
LOG.debug('Set up fixture %r', fixture.__tobiko_fixture_name__)
LOG.debug('Set up fixture %r', get_fixture_name(fixture))
fixture.setUp()
return fixture
def cleanup_fixture(obj, manager=None):
fixture = get_fixture(obj, manager=manager)
LOG.debug('Clean up fixture %r', fixture.__tobiko_fixture_name__)
LOG.debug('Clean up fixture %r', get_fixture_name(fixture))
fixture.cleanUp()
return fixture

@ -15,7 +15,6 @@ from __future__ import absolute_import
import abc
import fixtures
from oslo_log import log
import tobiko
@ -46,10 +45,7 @@ class OpenstackClientFixture(tobiko.SharedFixture):
def setup_session(self):
session_fixture = self.session_fixture
if session_fixture:
if not isinstance(session_fixture, fixtures.Fixture):
session_fixture = tobiko.get_fixture(session_fixture)
session_fixture.setUp()
self.session = session_fixture.session
self.session = tobiko.setup_fixture(session_fixture).session
elif not self.session:
self.session = keystone.get_keystone_session()

@ -13,6 +13,7 @@
# under the License.
from __future__ import absolute_import
import fixtures
import mock
import tobiko
@ -48,7 +49,13 @@ class FixtureManagerTest(unit.TobikoUnitTest):
fixture = tobiko.get_fixture(obj)
self.assertIsInstance(fixture, fixture_type)
self.assertIs(fixture, tobiko.get_fixture(obj))
self.assertIs(fixture, tobiko.get_fixture(MY_FIXTURE_NAME))
if isinstance(obj, fixtures.Fixture):
self.assertIs(obj, fixture)
else:
self.assertIs(fixture, tobiko.get_fixture(MY_FIXTURE_NAME))
def test_get_fixture_by_instance(self):
self._test_get_fixture(MyFixture(), fixture_type=MyFixture)
def test_remove_fixture_by_name(self):
self._test_remove_fixture(MY_FIXTURE_NAME)
@ -75,9 +82,12 @@ class FixtureManagerTest(unit.TobikoUnitTest):
def test_setup_fixture_by_type(self):
self._test_setup_fixture(MyFixture)
def test_setup_fixture_by_instance(self):
self._test_setup_fixture(MyFixture())
def _test_setup_fixture(self, obj):
result = tobiko.setup_fixture(obj)
self.assertIs(tobiko.get_fixture(MY_FIXTURE_NAME), result)
self.assertIs(tobiko.get_fixture(obj), result)
result.setup_fixture.assert_called_once_with()
def test_cleanup_fixture_by_name(self):
@ -86,9 +96,12 @@ class FixtureManagerTest(unit.TobikoUnitTest):
def test_cleanup_fixture_by_type(self):
self._test_cleanup_fixture(MyFixture)
def test_cleanup_fixture_by_instance(self):
self._test_cleanup_fixture(MyFixture())
def _test_cleanup_fixture(self, obj):
result = tobiko.cleanup_fixture(obj)
self.assertIs(tobiko.get_fixture(MY_FIXTURE_NAME), result)
self.assertIs(tobiko.get_fixture(obj), result)
result.cleanup_fixture.assert_called_once_with()
def test_list_required_fixtures_from_module(self):

Loading…
Cancel
Save