Use more specific asserts in tests

Use asserts with more specific assert methods, e.g.: from
assertTrue(sth == None) to assertIsNone(*),
assertTrue(isinstance(inst, type)) to assertIsInstace(inst, type) or
assertTrue(not sth) to assertFalse(sth).

This change ensures a better description will be shown on fail. This is
the recommended way to use assertions provided by testtools.

Change-Id: Ia4a34e0145176e077bbf7e9ed6f69d815c6ed840
Co-authored-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
Gábor Antal 2016-08-30 19:09:20 +02:00 committed by Stephen Finucane
parent aa7c3aef95
commit 03b9937884
12 changed files with 26 additions and 27 deletions

View File

@ -392,7 +392,7 @@ class APICoverage(object):
cover_api = None
def test_api_methods(self):
self.assertTrue(self.cover_api is not None)
self.assertIsNotNone(self.cover_api)
api_methods = [x for x in dir(self.cover_api)
if not x.startswith('_')]
test_methods = [x[5:] for x in dir(self)

View File

@ -132,8 +132,8 @@ class AggregateObjectDbTestCase(test.NoDBTestCase):
cell_db_agg = aggregate_obj.Aggregate.get_by_id(
self.context, ca2['id'])
self.assertEqual(api_db_agg.in_api, True)
self.assertEqual(cell_db_agg.in_api, False)
self.assertTrue(api_db_agg.in_api)
self.assertFalse(cell_db_agg.in_api)
def test_aggregate_get_from_db(self):
result = _create_aggregate_with_hosts(self.context)

View File

@ -428,7 +428,7 @@ class EvacuateTestV229(EvacuateTestV214):
force = mock_evacuate.call_args_list[0][0][5]
self.assertEqual(CONF.password_length, len(admin_pass))
self.assertIsNone(on_shared_storage)
self.assertEqual(False, force)
self.assertFalse(force)
def test_evacuate_with_valid_instance(self):
admin_pass = 'MyNewPass'
@ -442,7 +442,7 @@ class EvacuateTestV229(EvacuateTestV214):
self._get_evacuate_response({'host': 'my-host',
'force': 'true'})
force = mock_evacuate.call_args_list[0][0][5]
self.assertEqual(True, force)
self.assertTrue(force)
def test_forced_evacuate_with_no_host_provided(self):
self._check_evacuate_failure(webob.exc.HTTPBadRequest,

View File

@ -70,7 +70,7 @@ class HideServerAddressesTestV21(test.TestCase):
return server.get('addresses', SENTINEL)
def _check_addresses(self, addresses, exists):
self.assertTrue(addresses is not SENTINEL)
self.assertIsNot(addresses, SENTINEL)
if exists:
self.assertTrue(addresses)
else:

View File

@ -704,7 +704,7 @@ class ServersControllerTest(ControllerTest):
def test_tenant_id_filter_no_admin_context(self):
def fake_get_all(context, search_opts=None, **kwargs):
self.assertNotEqual(search_opts, None)
self.assertIsNotNone(search_opts)
self.assertEqual(search_opts['project_id'], 'fake')
return [fakes.stub_instance_obj(100)]

View File

@ -299,7 +299,7 @@ class _TestPciDeviceObject(object):
def test_pci_device_not_equivalent_with_not_pci_device(self):
pci_device1 = pci_device.PciDevice.create(None, dev_dict)
self.assertNotEqual(pci_device1, None)
self.assertIsNotNone(pci_device1)
self.assertNotEqual(pci_device1, 'foo')
self.assertNotEqual(pci_device1, 1)
self.assertNotEqual(pci_device1, objects.PciDeviceList())

View File

@ -312,8 +312,8 @@ class _TestRequestSpecObject(object):
filter_properties, instance_group, instance.availability_zone)
# Make sure that all fields are set using that helper method
for field in [f for f in spec.obj_fields if f != 'id']:
self.assertEqual(True, spec.obj_attr_is_set(field),
'Field: %s is not set' % field)
self.assertTrue(spec.obj_attr_is_set(field),
'Field: %s is not set' % field)
# just making sure that the context is set by the method
self.assertEqual(ctxt, spec._context)

View File

@ -131,15 +131,14 @@ class PciDeviceStatsTestCase(test.NoDBTestCase):
set(['v1', 'v2', 'v3']))
def test_support_requests(self):
self.assertEqual(self.pci_stats.support_requests(pci_requests),
True)
self.assertTrue(self.pci_stats.support_requests(pci_requests))
self.assertEqual(len(self.pci_stats.pools), 3)
self.assertEqual(set([d['count'] for d in self.pci_stats]),
set((1, 2)))
def test_support_requests_failed(self):
self.assertEqual(
self.pci_stats.support_requests(pci_requests_multiple), False)
self.assertFalse(
self.pci_stats.support_requests(pci_requests_multiple))
self.assertEqual(len(self.pci_stats.pools), 3)
self.assertEqual(set([d['count'] for d in self.pci_stats]),
set([1, 2]))

View File

@ -204,18 +204,18 @@ class IsAdminCheckTestCase(test.NoDBTestCase):
def test_call_true(self):
check = policy.IsAdminCheck('is_admin', 'True')
self.assertEqual(check('target', dict(is_admin=True),
policy._ENFORCER), True)
self.assertEqual(check('target', dict(is_admin=False),
policy._ENFORCER), False)
self.assertTrue(check('target', dict(is_admin=True),
policy._ENFORCER))
self.assertFalse(check('target', dict(is_admin=False),
policy._ENFORCER))
def test_call_false(self):
check = policy.IsAdminCheck('is_admin', 'False')
self.assertEqual(check('target', dict(is_admin=True),
policy._ENFORCER), False)
self.assertEqual(check('target', dict(is_admin=False),
policy._ENFORCER), True)
self.assertFalse(check('target', dict(is_admin=True),
policy._ENFORCER))
self.assertTrue(check('target', dict(is_admin=False),
policy._ENFORCER))
class AdminRolePolicyTestCase(test.NoDBTestCase):

View File

@ -10967,8 +10967,8 @@ class LibvirtConnTestCase(test.NoDBTestCase):
drvr.init_host("wibble")
drvr.get_num_instances()
self.assertTrue(not service_mock.disabled and
service_mock.disabled_reason is None)
self.assertFalse(service_mock.disabled)
self.assertIsNone(service_mock.disabled_reason)
@mock.patch.object(objects.Instance, 'save')
def test_immediate_delete(self, mock_save):

View File

@ -692,7 +692,7 @@ class LibvirtVifTestCase(test.NoDBTestCase):
doc = etree.fromstring(xml)
bandwidth = doc.find('./devices/interface/bandwidth')
self.assertNotEqual(bandwidth, None)
self.assertIsNotNone(bandwidth)
inbound = bandwidth.find('inbound')
self.assertEqual(inbound.get("average"),

View File

@ -35,8 +35,8 @@ class TestIptablesFirewallDriver(test.NoDBTestCase):
self.driver.__init__()
self.assertEqual({}, self.driver.instance_info)
self.assertEqual(False, self.driver.dhcp_create)
self.assertEqual(False, self.driver.dhcp_created)
self.assertFalse(self.driver.dhcp_create)
self.assertFalse(self.driver.dhcp_created)
self.assertEqual(iptm_mock, self.driver.iptables)
# NOTE(jaypipes): Here we are not testing the IptablesManager