replace assertNotEqual(*,None) to assertIsNotNone

replace assertNotEqual(*, None) with assertIsNotNone in tests to have
more clear messages in case of failure.add hacking for assertNotEqual
(*, None) and unit hacking unittest.

Change-Id: I16607be4830e39cc26a5d2cb3c12a0c41fa1d599
This commit is contained in:
huang.huayong 2016-07-13 12:25:43 +08:00
parent ba0c5dabfe
commit 26c1d50386
3 changed files with 29 additions and 0 deletions

View File

@ -21,6 +21,7 @@ Rally Specific Commandments
* [N322] - Ensure that ``assertEqual(A, None)`` and ``assertEqual(None, A)`` are not used
* [N323] - Ensure that ``assertTrue/assertFalse(A in/not in B)`` are not used with collection contents
* [N324] - Ensure that ``assertEqual(A in/not in B, True/False)`` and ``assertEqual(True/False, A in/not in B)`` are not used with collection contents
* [N325] - Ensure that ``assertNotEqual(A, None)`` and ``assertNotEqual(None, A)`` are not used
* [N340] - Ensure that we are importing always ``from rally import objects``
* [N341] - Ensure that we are importing oslo_xyz packages instead of deprecated oslo.xyz ones
* [N350] - Ensure that single quotes are not used

View File

@ -36,6 +36,9 @@ re_assert_equal_type = re.compile(
r"(\w|\.|\'|\"|\[|\])+\)")
re_assert_equal_end_with_none = re.compile(r"assertEqual\(.*?,\s+None\)$")
re_assert_equal_start_with_none = re.compile(r"assertEqual\(None,")
re_assert_not_equal_end_with_none = re.compile(
r"assertNotEqual\(.*?,\s+None\)$")
re_assert_not_equal_start_with_none = re.compile(r"assertNotEqual\(None,")
re_assert_true_false_with_in_or_not_in = re.compile(
r"assert(True|False)\("
r"(\w|[][.'\"])+( not)? in (\w|[][.'\",])+(, .*)?\)")
@ -277,6 +280,20 @@ def assert_equal_in(logical_line, physical_line, filename):
"collection contents.")
@skip_ignored_lines
def assert_not_equal_none(logical_line, physical_line, filename):
"""Check for assertNotEqual(A, None) or assertEqual(None, A) sentences
N325
"""
res = (re_assert_not_equal_start_with_none.search(logical_line) or
re_assert_not_equal_end_with_none.search(logical_line))
if res:
yield (0, "N325 assertNotEqual(A, None) or assertNotEqual(None, A) "
"sentences not allowed, you should use assertIsNotNone(A) "
"instead.")
@skip_ignored_lines
def check_no_direct_rally_objects_import(logical_line, physical_line,
filename):

View File

@ -170,6 +170,17 @@ class HackingTestCase(test.TestCase):
self.assertEqual(
len(list(checks.assert_equal_none(line, line, "f"))), result)
@ddt.data(
{"line": "self.assertNotEqual(A, None)", "result": 1},
{"line": "self.assertNotEqual(None, A)", "result": 1},
{"line": "self.assertIsNotNone()", "result": 0}
)
@ddt.unpack
def test_assert_not_equal_none(self, line, result):
self.assertEqual(
len(list(checks.assert_not_equal_none(line, line, "f"))), result)
def test_assert_true_or_false_with_in_or_not_in(self):
good_lines = [
"self.assertTrue(any(A > 5 for A in B))",