Replace assertEqual(None, *) with assertIsNone in tests

Replace assertEqual(None, *) with assertIsNone in tests to have
more clear messages in case of failure.

Change-Id: I0d38a82e78fbe94657ab9a71c08422007843d179
This commit is contained in:
zhang-jinnan 2014-02-14 13:14:24 +08:00
parent fac4532c8f
commit 33a459c2c3
13 changed files with 41 additions and 12 deletions

View File

@ -28,6 +28,8 @@ Nova Specific Commandments
assertIsInstance(A, B).
- [N317] Change assertEqual(type(A), B) by optimal assert like
assertIsInstance(A, B)
- [N318] Change assertEqual(A, None) or assertEqual(None, A) by optimal assert like
assertIsNone(A)
Creating Unit Tests
-------------------

View File

@ -44,6 +44,10 @@ asse_trueinst_re = re.compile(
asse_equal_type_re = re.compile(
r"(.)*assertEqual\(type\((\w|\.|\'|\"|\[|\])+\), "
"(\w|\.|\'|\"|\[|\])+\)")
asse_equal_end_with_none_re = re.compile(
r"(.)*assertEqual\((\w|\.|\'|\"|\[|\])+, None\)")
asse_equal_start_with_none_re = re.compile(
r"(.)*assertEqual\(None, (\w|\.|\'|\"|\[|\])+\)")
def import_no_db_in_virt(logical_line, filename):
@ -177,6 +181,18 @@ def assert_equal_type(logical_line):
yield (0, "N317: assertEqual(type(A), B) sentences not allowed")
def assert_equal_none(logical_line):
"""Check for assertEqual(A, None) or assertEqual(None, A) sentences
N318
"""
res = (asse_equal_start_with_none_re.match(logical_line) or
asse_equal_end_with_none_re.match(logical_line))
if res:
yield (0, "N318: assertEqual(A, None) or assertEqual(None, A) "
"sentences not allowed")
def factory(register):
register(import_no_db_in_virt)
register(no_db_session_in_public_api)
@ -188,3 +204,4 @@ def factory(register):
register(no_author_tags)
register(assert_true_instance)
register(assert_equal_type)
register(assert_equal_none)

View File

@ -130,7 +130,7 @@ class AggregateTestCase(test.NoDBTestCase):
def stub_create_aggregate(context, name, availability_zone):
self.assertEqual(context, self.context, "context")
self.assertEqual("test", name, "name")
self.assertEqual(None, availability_zone, "availability_zone")
self.assertIsNone(availability_zone, "availability_zone")
return AGGREGATE
self.stubs.Set(self.controller.api, "create_aggregate",
stub_create_aggregate)

View File

@ -129,7 +129,7 @@ class AggregateTestCase(test.NoDBTestCase):
def stub_create_aggregate(context, name, availability_zone):
self.assertEqual(context, self.context, "context")
self.assertEqual("test", name, "name")
self.assertEqual(None, availability_zone, "availability_zone")
self.assertIsNone(availability_zone, "availability_zone")
return AGGREGATE
self.stubs.Set(self.controller.api, "create_aggregate",
stub_create_aggregate)

View File

@ -339,7 +339,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
self.mox.ReplayAll()
self.compute._init_instance(self.context, instance)
self.assertEqual(None, instance.task_state)
self.assertIsNone(instance.task_state)
def test_init_instance_reverts_crashed_migration_from_active(self):
self._test_init_instance_reverts_crashed_migrations(

View File

@ -60,7 +60,7 @@ class ComputeVMModeTest(test.NoDBTestCase):
def test_name_none(self):
mode = vm_mode.name(None)
self.assertEqual(None, mode)
self.assertIsNone(mode)
def test_name_invalid(self):
self.assertRaises(exception.Invalid, vm_mode.name, 'invalid')

View File

@ -67,7 +67,7 @@ class _TestDNSDomain(object):
dns_domain.DNSDomain.delete_by_domain(self.context.elevated(),
'domain')
dnsd = dns_domain.DNSDomain.get_by_domain(self.context, 'domain')
self.assertEqual(None, dnsd)
self.assertIsNone(dnsd)
def test_get_all(self):
with mock.patch.object(db, 'dnsdomain_get_all') as get:

View File

@ -122,7 +122,7 @@ class _TestFixedIPObject(object):
'instance'])
self._compare(fixedip, db_fixed)
self.assertEqual(db_fixed['network']['uuid'], fixedip.network.uuid)
self.assertEqual(None, fixedip.instance)
self.assertIsNone(fixedip.instance)
self.assertFalse(network_get.called)
self.assertFalse(instance_get.called)
@ -209,7 +209,7 @@ class _TestFixedIPObject(object):
fixedip.obj_reset_changes()
fixedip.disassociate()
disassociate.assert_called_once_with(self.context, '1.2.3.4')
self.assertEqual(None, fixedip.instance_uuid)
self.assertIsNone(fixedip.instance_uuid)
@mock.patch('nova.db.fixed_ip_get_all')
def test_get_all(self, get_all):

View File

@ -70,3 +70,13 @@ class HackingTestCase(test.NoDBTestCase):
self.assertEqual(
len(list(checks.assert_equal_type("self.assertTrue()"))), 0)
def test_assert_equal_none(self):
self.assertEqual(len(list(checks.assert_equal_none(
"self.assertEqual(A, None)"))), 1)
self.assertEqual(len(list(checks.assert_equal_none(
"self.assertEqual(None, A)"))), 1)
self.assertEqual(
len(list(checks.assert_equal_none("self.assertIsNone()"))), 0)

View File

@ -194,7 +194,7 @@ class BareMetalDriverWithDBTestCase(bm_db_base.BMDBTestCase):
self.assertEqual(row['instance_name'], node['instance']['hostname'])
instance = main_db.instance_get_by_uuid(self.context,
node['instance']['uuid'])
self.assertEqual(instance['default_ephemeral_device'], None)
self.assertIsNone(instance['default_ephemeral_device'])
def _test_rebuild(self, ephemeral):
node = self._create_node(ephemeral=ephemeral)

View File

@ -22,7 +22,7 @@ class CpuSetTestCase(test.NoDBTestCase):
def test_get_cpuset_ids_none_returns_none(self):
self.flags(vcpu_pin_set=None)
cpuset_ids = cpu.get_cpuset_ids()
self.assertEqual(None, cpuset_ids)
self.assertIsNone(cpuset_ids)
def test_get_cpuset_ids_valid_syntax_works(self):
self.flags(vcpu_pin_set="1")

View File

@ -161,7 +161,7 @@ class TestDiskImage(test.NoDBTestCase):
self.assertEqual(diskimage._mounter, fakemount)
self.assertEqual(dev, '/dev/fake')
diskimage.umount()
self.assertEqual(diskimage._mounter, None)
self.assertIsNone(diskimage._mounter)
@mock.patch('__builtin__.open')
def test_teardown(self, mock_open):
@ -181,7 +181,7 @@ class TestDiskImage(test.NoDBTestCase):
self.assertEqual(diskimage._mounter, fakemount)
self.assertEqual(dev, '/dev/fake')
diskimage.teardown()
self.assertEqual(diskimage._mounter, None)
self.assertIsNone(diskimage._mounter)
class TestVirtDisk(test.NoDBTestCase):

View File

@ -1917,7 +1917,7 @@ class ImportMigratedDisksTestCase(VMUtilsTestBase):
session = mock.Mock()
vdi_ref = 'vdi_ref'
vdi_rec = {'sm_config': {}}
self.assertEqual(None, vm_utils._get_vhd_parent_uuid(session,
self.assertIsNone(vm_utils._get_vhd_parent_uuid(session,
vdi_ref,
vdi_rec))
self.assertFalse(session.call_xenapi.called)