Add fixture_id parameter to allow multiple fixtures of the same class
Change-Id: Ida615ac7f2cdec4a54f3d46aa510ccfc862ece3a
This commit is contained in:
parent
85fbbdae28
commit
65ad99bec5
@ -36,7 +36,7 @@ def is_fixture(obj):
|
||||
(inspect.isclass(obj) and issubclass(obj, fixtures.Fixture)))
|
||||
|
||||
|
||||
def get_fixture(obj, manager=None):
|
||||
def get_fixture(obj, fixture_id=None, manager=None):
|
||||
'''Returns a fixture identified by given :param obj:
|
||||
|
||||
It returns registered fixture for given :param obj:. If none has been
|
||||
@ -58,7 +58,7 @@ def get_fixture(obj, manager=None):
|
||||
return obj
|
||||
else:
|
||||
manager = manager or FIXTURES
|
||||
return manager.get_fixture(obj)
|
||||
return manager.get_fixture(obj, fixture_id=fixture_id)
|
||||
|
||||
|
||||
def get_fixture_name(obj):
|
||||
@ -90,31 +90,31 @@ def get_fixture_dir(obj):
|
||||
return os.path.dirname(inspect.getfile(get_fixture_class(obj)))
|
||||
|
||||
|
||||
def remove_fixture(obj, manager=None):
|
||||
def remove_fixture(obj, fixture_id=None, manager=None):
|
||||
'''Unregister fixture identified by given :param obj: if any'''
|
||||
manager = manager or FIXTURES
|
||||
return manager.remove_fixture(obj)
|
||||
return manager.remove_fixture(obj, fixture_id=fixture_id)
|
||||
|
||||
|
||||
def setup_fixture(obj, manager=None):
|
||||
def setup_fixture(obj, fixture_id=None, manager=None):
|
||||
'''Get registered fixture and setup it up'''
|
||||
fixture = get_fixture(obj, manager=manager)
|
||||
fixture = get_fixture(obj, fixture_id=fixture_id, manager=manager)
|
||||
with _exception.handle_multiple_exceptions():
|
||||
fixture.setUp()
|
||||
return fixture
|
||||
|
||||
|
||||
def reset_fixture(obj, manager=None):
|
||||
def reset_fixture(obj, fixture_id=None, manager=None):
|
||||
'''Get registered fixture and reset it'''
|
||||
fixture = get_fixture(obj, manager=manager)
|
||||
fixture = get_fixture(obj, fixture_id=fixture_id, manager=manager)
|
||||
with _exception.handle_multiple_exceptions():
|
||||
fixture.reset()
|
||||
return fixture
|
||||
|
||||
|
||||
def cleanup_fixture(obj, manager=None):
|
||||
def cleanup_fixture(obj, fixture_id=None, manager=None):
|
||||
'''Get registered fixture and clean it up'''
|
||||
fixture = get_fixture(obj, manager=manager)
|
||||
fixture = get_fixture(obj, fixture_id=fixture_id, manager=manager)
|
||||
with _exception.handle_multiple_exceptions():
|
||||
fixture.cleanUp()
|
||||
return fixture
|
||||
@ -302,8 +302,10 @@ class FixtureManager(object):
|
||||
def __init__(self):
|
||||
self.fixtures = {}
|
||||
|
||||
def get_fixture(self, obj, init=None):
|
||||
def get_fixture(self, obj, fixture_id=None, init=None):
|
||||
name, obj = get_name_and_object(obj)
|
||||
if fixture_id:
|
||||
name += '-' + str(fixture_id)
|
||||
fixture = self.fixtures.get(name)
|
||||
if fixture is None:
|
||||
init = init or self.init_fixture
|
||||
@ -314,8 +316,10 @@ class FixtureManager(object):
|
||||
def init_fixture(self, obj, name):
|
||||
return init_fixture(obj=obj, name=name)
|
||||
|
||||
def remove_fixture(self, obj):
|
||||
def remove_fixture(self, obj, fixture_id=None):
|
||||
name = get_object_name(obj)
|
||||
if fixture_id:
|
||||
name += '-' + str(fixture_id)
|
||||
return self.fixtures.pop(name, None)
|
||||
|
||||
|
||||
|
@ -74,6 +74,13 @@ class GetFixtureTest(unit.TobikoUnitTest):
|
||||
fixture.setup_fixture.assert_not_called()
|
||||
fixture.cleanup_fixture.assert_not_called()
|
||||
|
||||
for fixture_id in range(2):
|
||||
other = tobiko.get_fixture(obj, fixture_id=fixture_id)
|
||||
if isinstance(obj, fixtures.Fixture) or not fixture_id:
|
||||
self.assertIs(fixture, other)
|
||||
else:
|
||||
self.assertIsNot(fixture, other)
|
||||
|
||||
|
||||
class GetFixtureNameTest(unit.TobikoUnitTest):
|
||||
|
||||
@ -129,11 +136,18 @@ class RemoveFixtureTest(unit.TobikoUnitTest):
|
||||
def test_with_type(self):
|
||||
self._test_remove_fixture(MyFixture)
|
||||
|
||||
def _test_remove_fixture(self, obj):
|
||||
fixture = tobiko.get_fixture(obj)
|
||||
result = tobiko.remove_fixture(obj)
|
||||
def test_with_name_and_fixture_id(self):
|
||||
self._test_remove_fixture(canonical_name(MyFixture), fixture_id=5)
|
||||
|
||||
def test_with_type_and_fixture_id(self):
|
||||
self._test_remove_fixture(MyFixture, fixture_id=6)
|
||||
|
||||
def _test_remove_fixture(self, obj, fixture_id=None):
|
||||
fixture = tobiko.get_fixture(obj, fixture_id=fixture_id)
|
||||
result = tobiko.remove_fixture(obj, fixture_id=fixture_id)
|
||||
self.assertIs(fixture, result)
|
||||
self.assertIsNot(fixture, tobiko.get_fixture(obj))
|
||||
self.assertIsNot(fixture, tobiko.get_fixture(obj,
|
||||
fixture_id=fixture_id))
|
||||
fixture.setup_fixture.assert_not_called()
|
||||
fixture.cleanup_fixture.assert_not_called()
|
||||
|
||||
@ -149,9 +163,18 @@ class SetupFixtureTest(unit.TobikoUnitTest):
|
||||
def test_with_instance(self):
|
||||
self._test_setup_fixture(MyFixture2())
|
||||
|
||||
def _test_setup_fixture(self, obj):
|
||||
result = tobiko.setup_fixture(obj)
|
||||
self.assertIs(tobiko.get_fixture(obj), result)
|
||||
def test_with_name_and_fixture_id(self):
|
||||
self._test_setup_fixture(canonical_name(MyFixture), fixture_id=5)
|
||||
|
||||
def test_with_type_and_fixture_id(self):
|
||||
self._test_setup_fixture(MyFixture, fixture_id=6)
|
||||
|
||||
def test_with_instance_and_fixture_id(self):
|
||||
self._test_setup_fixture(MyFixture2(), fixture_id=7)
|
||||
|
||||
def _test_setup_fixture(self, obj, fixture_id=None):
|
||||
result = tobiko.setup_fixture(obj, fixture_id=fixture_id)
|
||||
self.assertIs(tobiko.get_fixture(obj, fixture_id=fixture_id), result)
|
||||
result.setup_fixture.assert_called_once_with()
|
||||
result.cleanup_fixture.assert_not_called()
|
||||
|
||||
@ -167,6 +190,15 @@ class ResetFixtureTest(unit.TobikoUnitTest):
|
||||
def test_with_instance(self):
|
||||
self._test_reset_fixture(MyFixture2())
|
||||
|
||||
def test_with_name_and_fixture_id(self):
|
||||
self._test_reset_fixture(canonical_name(MyFixture), fixture_id=5)
|
||||
|
||||
def test_with_type_and_fixture_id(self):
|
||||
self._test_reset_fixture(MyFixture, fixture_id=6)
|
||||
|
||||
def test_with_instance_and_fixture_id(self):
|
||||
self._test_reset_fixture(MyFixture(), fixture_id=7)
|
||||
|
||||
def test_after_setup(self):
|
||||
fixture = MyFixture2()
|
||||
fixture.setUp()
|
||||
@ -178,9 +210,9 @@ class ResetFixtureTest(unit.TobikoUnitTest):
|
||||
fixture.cleanUp()
|
||||
self._test_reset_fixture(fixture)
|
||||
|
||||
def _test_reset_fixture(self, obj, should_clean=True):
|
||||
result = tobiko.reset_fixture(obj)
|
||||
self.assertIs(tobiko.get_fixture(obj), result)
|
||||
def _test_reset_fixture(self, obj, fixture_id=None, should_clean=True):
|
||||
result = tobiko.reset_fixture(obj, fixture_id=fixture_id)
|
||||
self.assertIs(tobiko.get_fixture(obj, fixture_id=fixture_id), result)
|
||||
result.setup_fixture.assert_called_once_with()
|
||||
if should_clean:
|
||||
result.cleanup_fixture.assert_called_once_with()
|
||||
@ -208,8 +240,18 @@ class FailingSetupFixtureWhenFailingTest(unit.TobikoUnitTest):
|
||||
def test_with_instance(self):
|
||||
self._test_setup_fixture(FailingFixture())
|
||||
|
||||
def _test_setup_fixture(self, obj):
|
||||
ex = self.assertRaises(RuntimeError, tobiko.setup_fixture, obj)
|
||||
def test_with_name_and_fixture_id(self):
|
||||
self._test_setup_fixture(canonical_name(FailingFixture), fixture_id=5)
|
||||
|
||||
def test_with_type_and_fixture_id(self):
|
||||
self._test_setup_fixture(FailingFixture, fixture_id=6)
|
||||
|
||||
def test_with_instance_and_fixture_id(self):
|
||||
self._test_setup_fixture(FailingFixture(), fixture_id=7)
|
||||
|
||||
def _test_setup_fixture(self, obj, fixture_id=None):
|
||||
ex = self.assertRaises(
|
||||
RuntimeError, tobiko.setup_fixture, obj, fixture_id=fixture_id)
|
||||
self.assertEqual('raised by setup_fixture', str(ex))
|
||||
|
||||
|
||||
@ -224,9 +266,18 @@ class CleanupFixtureTest(unit.TobikoUnitTest):
|
||||
def test_with_instance(self):
|
||||
self._test_cleanup_fixture(MyFixture())
|
||||
|
||||
def _test_cleanup_fixture(self, obj):
|
||||
result = tobiko.cleanup_fixture(obj)
|
||||
self.assertIs(tobiko.get_fixture(obj), result)
|
||||
def test_with_name_and_fixture_id(self):
|
||||
self._test_cleanup_fixture(canonical_name(MyFixture), fixture_id=5)
|
||||
|
||||
def test_with_type_and_fixture_id(self):
|
||||
self._test_cleanup_fixture(MyFixture, fixture_id=6)
|
||||
|
||||
def test_with_instance_and_fixture_id(self):
|
||||
self._test_cleanup_fixture(MyFixture(), fixture_id=7)
|
||||
|
||||
def _test_cleanup_fixture(self, obj, fixture_id=None):
|
||||
result = tobiko.cleanup_fixture(obj, fixture_id=fixture_id)
|
||||
self.assertIs(tobiko.get_fixture(obj, fixture_id=fixture_id), result)
|
||||
result.setup_fixture.assert_not_called()
|
||||
result.cleanup_fixture.assert_called_once_with()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user