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: Ice018dfd3e48326ce9cac58342d03979559b9e82
This commit is contained in:
gengchc2 2016-09-22 16:09:15 +08:00
parent a780d306b9
commit f276a96d84
5 changed files with 27 additions and 3 deletions

View File

@ -32,6 +32,7 @@ Neutron Specific Commandments
- [N343] Production code must not import from neutron.tests.*
- [N344] Python 3: Do not use filter(lambda obj: test(obj), data). Replace it
with [obj for obj in data if test(obj)].
- [N345] Detect wrong usage with assertEqual(None, A) and assertEqual(A, None).
Creating Unit Tests
-------------------

View File

@ -414,6 +414,19 @@ def check_python3_no_filter(logical_line):
yield(0, msg)
@flake8ext
def check_assertIsNone(logical_line, filename):
"""N345 - Enforce using assertIsNone."""
if 'neutron/tests/' in filename:
asse_eq_end_with_none_re = re.compile(r"assertEqual\(.*?,\s+None\)$")
asse_eq_start_with_none_re = re.compile(r"assertEqual\(None,")
res = (asse_eq_start_with_none_re.search(logical_line) or
asse_eq_end_with_none_re.search(logical_line))
if res:
yield (0, "N345: assertEqual(A, None) or assertEqual(None, A) "
"sentences not allowed")
def factory(register):
register(validate_log_translations)
register(use_jsonutils)
@ -435,3 +448,4 @@ def factory(register):
register(check_delayed_string_interpolation)
register(check_no_imports_from_tests)
register(check_python3_no_filter)
register(check_assertIsNone)

View File

@ -100,4 +100,4 @@ class FloatingIPTestJSON(base.BaseNetworkTest):
# disassociate
body = self.client.update_floatingip(body['floatingip']['id'],
port_id=None)
self.assertEqual(None, body['floatingip']['port_id'])
self.assertIsNone(body['floatingip']['port_id'])

View File

@ -756,8 +756,7 @@ class L3HATestCase(L3HATestFramework):
def test_get_active_host_for_ha_router(self):
router = self._create_router()
self.assertEqual(
None,
self.assertIsNone(
self.plugin.get_active_host_for_ha_router(
self.admin_ctx, router['id']))
self.plugin.update_routers_states(

View File

@ -360,6 +360,16 @@ class HackingTestCase(base.BaseTestCase):
self.assertLinePasses(f, "filter(function, range(0,10))")
self.assertLinePasses(f, "lambda x, y: x+y")
def test_check_assertIsNone(self):
self.assertEqual(1, len(list(checks.check_assertIsNone(
"self.assertEqual(A, None)", "neutron/tests/test_assert.py"))))
self.assertEqual(1, len(list(checks.check_assertIsNone(
"self.assertEqual(None, A)", "neutron/tests/test_assert.py"))))
self.assertEqual(0, len(list(checks.check_assertIsNone(
"self.assertIsNone()", "neutron/tests/test_assert.py"))))
# The following is borrowed from hacking/tests/test_doctest.py.
# Tests defined in docstring is easier to understand