Add fixture_id parameter to allow multiple fixtures of the same class

Change-Id: Ida615ac7f2cdec4a54f3d46aa510ccfc862ece3a
This commit is contained in:
Federico Ressi 2020-07-01 13:44:50 +02:00
parent 85fbbdae28
commit 65ad99bec5
2 changed files with 82 additions and 27 deletions

View File

@ -36,7 +36,7 @@ def is_fixture(obj):
(inspect.isclass(obj) and issubclass(obj, fixtures.Fixture))) (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: '''Returns a fixture identified by given :param obj:
It returns registered fixture for given :param obj:. If none has been It returns registered fixture for given :param obj:. If none has been
@ -58,7 +58,7 @@ def get_fixture(obj, manager=None):
return obj return obj
else: else:
manager = manager or FIXTURES manager = manager or FIXTURES
return manager.get_fixture(obj) return manager.get_fixture(obj, fixture_id=fixture_id)
def get_fixture_name(obj): def get_fixture_name(obj):
@ -90,31 +90,31 @@ def get_fixture_dir(obj):
return os.path.dirname(inspect.getfile(get_fixture_class(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''' '''Unregister fixture identified by given :param obj: if any'''
manager = manager or FIXTURES 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''' '''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(): with _exception.handle_multiple_exceptions():
fixture.setUp() fixture.setUp()
return fixture return fixture
def reset_fixture(obj, manager=None): def reset_fixture(obj, fixture_id=None, manager=None):
'''Get registered fixture and reset it''' '''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(): with _exception.handle_multiple_exceptions():
fixture.reset() fixture.reset()
return fixture return fixture
def cleanup_fixture(obj, manager=None): def cleanup_fixture(obj, fixture_id=None, manager=None):
'''Get registered fixture and clean it up''' '''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(): with _exception.handle_multiple_exceptions():
fixture.cleanUp() fixture.cleanUp()
return fixture return fixture
@ -302,8 +302,10 @@ class FixtureManager(object):
def __init__(self): def __init__(self):
self.fixtures = {} 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) name, obj = get_name_and_object(obj)
if fixture_id:
name += '-' + str(fixture_id)
fixture = self.fixtures.get(name) fixture = self.fixtures.get(name)
if fixture is None: if fixture is None:
init = init or self.init_fixture init = init or self.init_fixture
@ -314,8 +316,10 @@ class FixtureManager(object):
def init_fixture(self, obj, name): def init_fixture(self, obj, name):
return init_fixture(obj=obj, name=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) name = get_object_name(obj)
if fixture_id:
name += '-' + str(fixture_id)
return self.fixtures.pop(name, None) return self.fixtures.pop(name, None)

View File

@ -74,6 +74,13 @@ class GetFixtureTest(unit.TobikoUnitTest):
fixture.setup_fixture.assert_not_called() fixture.setup_fixture.assert_not_called()
fixture.cleanup_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): class GetFixtureNameTest(unit.TobikoUnitTest):
@ -129,11 +136,18 @@ class RemoveFixtureTest(unit.TobikoUnitTest):
def test_with_type(self): def test_with_type(self):
self._test_remove_fixture(MyFixture) self._test_remove_fixture(MyFixture)
def _test_remove_fixture(self, obj): def test_with_name_and_fixture_id(self):
fixture = tobiko.get_fixture(obj) self._test_remove_fixture(canonical_name(MyFixture), fixture_id=5)
result = tobiko.remove_fixture(obj)
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.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.setup_fixture.assert_not_called()
fixture.cleanup_fixture.assert_not_called() fixture.cleanup_fixture.assert_not_called()
@ -149,9 +163,18 @@ class SetupFixtureTest(unit.TobikoUnitTest):
def test_with_instance(self): def test_with_instance(self):
self._test_setup_fixture(MyFixture2()) self._test_setup_fixture(MyFixture2())
def _test_setup_fixture(self, obj): def test_with_name_and_fixture_id(self):
result = tobiko.setup_fixture(obj) self._test_setup_fixture(canonical_name(MyFixture), fixture_id=5)
self.assertIs(tobiko.get_fixture(obj), result)
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.setup_fixture.assert_called_once_with()
result.cleanup_fixture.assert_not_called() result.cleanup_fixture.assert_not_called()
@ -167,6 +190,15 @@ class ResetFixtureTest(unit.TobikoUnitTest):
def test_with_instance(self): def test_with_instance(self):
self._test_reset_fixture(MyFixture2()) 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): def test_after_setup(self):
fixture = MyFixture2() fixture = MyFixture2()
fixture.setUp() fixture.setUp()
@ -178,9 +210,9 @@ class ResetFixtureTest(unit.TobikoUnitTest):
fixture.cleanUp() fixture.cleanUp()
self._test_reset_fixture(fixture) self._test_reset_fixture(fixture)
def _test_reset_fixture(self, obj, should_clean=True): def _test_reset_fixture(self, obj, fixture_id=None, should_clean=True):
result = tobiko.reset_fixture(obj) result = tobiko.reset_fixture(obj, fixture_id=fixture_id)
self.assertIs(tobiko.get_fixture(obj), result) self.assertIs(tobiko.get_fixture(obj, fixture_id=fixture_id), result)
result.setup_fixture.assert_called_once_with() result.setup_fixture.assert_called_once_with()
if should_clean: if should_clean:
result.cleanup_fixture.assert_called_once_with() result.cleanup_fixture.assert_called_once_with()
@ -208,8 +240,18 @@ class FailingSetupFixtureWhenFailingTest(unit.TobikoUnitTest):
def test_with_instance(self): def test_with_instance(self):
self._test_setup_fixture(FailingFixture()) self._test_setup_fixture(FailingFixture())
def _test_setup_fixture(self, obj): def test_with_name_and_fixture_id(self):
ex = self.assertRaises(RuntimeError, tobiko.setup_fixture, obj) 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)) self.assertEqual('raised by setup_fixture', str(ex))
@ -224,9 +266,18 @@ class CleanupFixtureTest(unit.TobikoUnitTest):
def test_with_instance(self): def test_with_instance(self):
self._test_cleanup_fixture(MyFixture()) self._test_cleanup_fixture(MyFixture())
def _test_cleanup_fixture(self, obj): def test_with_name_and_fixture_id(self):
result = tobiko.cleanup_fixture(obj) self._test_cleanup_fixture(canonical_name(MyFixture), fixture_id=5)
self.assertIs(tobiko.get_fixture(obj), result)
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.setup_fixture.assert_not_called()
result.cleanup_fixture.assert_called_once_with() result.cleanup_fixture.assert_called_once_with()