Setup required fixture by default

Change-Id: If8f79aaa90a97bbf469b0bb1f7d88d4d50e9dcb7
This commit is contained in:
Federico Ressi 2021-08-17 09:14:52 +02:00
parent 4233ae5ddf
commit 680f9c9ec2
3 changed files with 46 additions and 40 deletions

View File

@ -473,9 +473,10 @@ class FixtureProperty(property):
class RequiredFixtureProperty(object):
def __init__(self, fixture, **params):
def __init__(self, fixture, setup=True, **params):
self.fixture = fixture
self.fixture_params = params
self.setup = setup
def __get__(self, instance, _):
if instance is None:
@ -484,7 +485,15 @@ class RequiredFixtureProperty(object):
return self.get_fixture(instance)
def get_fixture(self, _instance):
return get_fixture(self.fixture, **self.fixture_params)
fixture = get_fixture(self.fixture, **self.fixture_params)
if self.setup:
setup_fixture(fixture)
if (hasattr(_instance, 'addCleanup') and
hasattr(_instance, 'getDetails')):
_instance.addCleanup(_detail.gather_details,
fixture.getDetails(),
_instance.getDetails())
return fixture
@property
def __tobiko_required_fixtures__(self):

View File

@ -32,7 +32,8 @@ CONF = config.CONF
class SSHClientFixtureTest(unit.TobikoUnitTest):
fixture = tobiko.required_fixture(ssh.SSHClientFixture)
fixture = tobiko.required_fixture(ssh.SSHClientFixture,
setup=False)
expected_host = None
expected_proxy_client = None

View File

@ -316,80 +316,76 @@ class MyRequiredFixture(MyBaseFixture):
pass
class MyRequiredSetupFixture(MyBaseFixture):
pass
class ListRequiredFixtureTest(unit.TobikoUnitTest):
class RequiredFixtureTest(unit.TobikoUnitTest):
required_fixture = tobiko.required_fixture(MyRequiredFixture)
required_setup_fixture = tobiko.required_setup_fixture(
MyRequiredSetupFixture)
required_fixture_no_setup = tobiko.required_fixture(
MyRequiredFixture, setup=False)
def test_with_module(self):
def test_list_required_fixtures_with_module(self):
module = sys.modules[__name__]
result = tobiko.list_required_fixtures([module])
self.assertEqual([], result)
def test_with_module_name(self):
def test_list_required_fixtures_with_module_name(self):
result = tobiko.list_required_fixtures([__name__])
self.assertEqual([], result)
def test_with_testcase_type(self):
result = tobiko.list_required_fixtures([ListRequiredFixtureTest])
self.assertEqual([canonical_name(MyRequiredFixture),
canonical_name(MyRequiredSetupFixture)], result)
def test_list_required_fixtures_with_testcase_type(self):
result = tobiko.list_required_fixtures([RequiredFixtureTest])
self.assertEqual([canonical_name(MyRequiredFixture)], result)
def test_with_testcase_name(self):
def test_list_required_fixtures_with_testcase_name(self):
result = tobiko.list_required_fixtures(
[canonical_name(ListRequiredFixtureTest)])
self.assertEqual([canonical_name(MyRequiredFixture),
canonical_name(MyRequiredSetupFixture)], result)
[canonical_name(RequiredFixtureTest)])
self.assertEqual([canonical_name(MyRequiredFixture)], result)
def test_with_unbound_method(self, fixture=MyFixture, fixture2=MyFixture2):
def test_list_required_fixtures_with_unbound_method(
self, fixture=MyFixture, fixture2=MyFixture2):
cls = RequiredFixtureTest
result = tobiko.list_required_fixtures(
[ListRequiredFixtureTest.test_with_unbound_method])
[cls.test_list_required_fixtures_with_unbound_method])
self.assertEqual([canonical_name(fixture),
canonical_name(fixture2),
canonical_name(MyRequiredFixture),
canonical_name(MyRequiredSetupFixture)], result)
canonical_name(MyRequiredFixture)], result)
def test_with_bound_method(self, fixture=MyFixture, fixture2=MyFixture2):
result = tobiko.list_required_fixtures([self.test_with_bound_method])
def test_list_required_fixtures_with_bound_method(
self, fixture=MyFixture, fixture2=MyFixture2):
result = tobiko.list_required_fixtures([
self.test_list_required_fixtures_with_bound_method])
self.assertEqual([canonical_name(fixture),
canonical_name(fixture2),
canonical_name(MyRequiredFixture),
canonical_name(MyRequiredSetupFixture)], result)
canonical_name(MyRequiredFixture)], result)
def test_with_method_name(self, fixture=MyFixture, fixture2=MyFixture2):
def test_list_required_fixtures_with_method_name(
self, fixture=MyFixture, fixture2=MyFixture2):
result = tobiko.list_required_fixtures([self.id()])
self.assertEqual([canonical_name(fixture),
canonical_name(fixture2),
canonical_name(MyRequiredFixture),
canonical_name(MyRequiredSetupFixture)], result)
canonical_name(MyRequiredFixture)], result)
def test_with_fixture_name(self):
def test_list_required_fixtures_with_fixture_name(self):
result = tobiko.list_required_fixtures([canonical_name(MyFixture)])
self.assertEqual([canonical_name(MyFixture)], result)
def test_with_fixture(self):
def test_list_required_fixtures_with_fixture(self):
result = tobiko.list_required_fixtures([MyFixture()])
self.assertEqual([canonical_name(MyFixture)], result)
def test_with_fixture_type(self):
def test__list_required_fixtures_with_fixture_type(self):
result = tobiko.list_required_fixtures([MyFixture])
self.assertEqual([canonical_name(MyFixture)], result)
def test_required_fixture_property(self):
def test_required_fixture_with_instance(self):
fixture = self.required_fixture
self.assertIsInstance(fixture, MyRequiredFixture)
fixture.setup_fixture.assert_not_called()
fixture.setup_fixture.assert_called()
fixture.cleanup_fixture.assert_not_called()
def test_required_setup_fixture_property(self):
fixture = self.required_setup_fixture
self.assertIsInstance(fixture, MyRequiredSetupFixture)
fixture.setup_fixture.assert_called_once_with()
def test_required_fixture_with_no_setup(self):
fixture = self.required_fixture_no_setup
self.assertIsInstance(fixture, MyRequiredFixture)
fixture.setup_fixture.assert_not_called()
fixture.cleanup_fixture.assert_not_called()