Commit c646729c authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #19594: Use specific asserts in unittest tests.

parent eda13afa
...@@ -307,7 +307,7 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing): ...@@ -307,7 +307,7 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing):
def test(self): def test(self):
pass pass
self.assertTrue(Foo('test').failureException is AssertionError) self.assertIs(Foo('test').failureException, AssertionError)
# "This class attribute gives the exception raised by the test() method. # "This class attribute gives the exception raised by the test() method.
# If a test framework needs to use a specialized exception, possibly to # If a test framework needs to use a specialized exception, possibly to
...@@ -325,7 +325,7 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing): ...@@ -325,7 +325,7 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing):
failureException = RuntimeError failureException = RuntimeError
self.assertTrue(Foo('test').failureException is RuntimeError) self.assertIs(Foo('test').failureException, RuntimeError)
Foo('test').run(result) Foo('test').run(result)
...@@ -348,7 +348,7 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing): ...@@ -348,7 +348,7 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing):
failureException = RuntimeError failureException = RuntimeError
self.assertTrue(Foo('test').failureException is RuntimeError) self.assertIs(Foo('test').failureException, RuntimeError)
Foo('test').run(result) Foo('test').run(result)
...@@ -660,7 +660,7 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing): ...@@ -660,7 +660,7 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing):
msg = e.args[0] msg = e.args[0]
else: else:
self.fail('assertSequenceEqual did not fail.') self.fail('assertSequenceEqual did not fail.')
self.assertTrue(len(msg) < len(diff)) self.assertLess(len(msg), len(diff))
self.assertIn(omitted, msg) self.assertIn(omitted, msg)
self.maxDiff = len(diff) * 2 self.maxDiff = len(diff) * 2
...@@ -670,7 +670,7 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing): ...@@ -670,7 +670,7 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing):
msg = e.args[0] msg = e.args[0]
else: else:
self.fail('assertSequenceEqual did not fail.') self.fail('assertSequenceEqual did not fail.')
self.assertTrue(len(msg) > len(diff)) self.assertGreater(len(msg), len(diff))
self.assertNotIn(omitted, msg) self.assertNotIn(omitted, msg)
self.maxDiff = None self.maxDiff = None
...@@ -680,7 +680,7 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing): ...@@ -680,7 +680,7 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing):
msg = e.args[0] msg = e.args[0]
else: else:
self.fail('assertSequenceEqual did not fail.') self.fail('assertSequenceEqual did not fail.')
self.assertTrue(len(msg) > len(diff)) self.assertGreater(len(msg), len(diff))
self.assertNotIn(omitted, msg) self.assertNotIn(omitted, msg)
def testTruncateMessage(self): def testTruncateMessage(self):
......
...@@ -1305,4 +1305,4 @@ class Test_TestLoader(unittest.TestCase): ...@@ -1305,4 +1305,4 @@ class Test_TestLoader(unittest.TestCase):
# "The default value is the TestSuite class" # "The default value is the TestSuite class"
def test_suiteClass__default_value(self): def test_suiteClass__default_value(self):
loader = unittest.TestLoader() loader = unittest.TestLoader()
self.assertTrue(loader.suiteClass is unittest.TestSuite) self.assertIs(loader.suiteClass, unittest.TestSuite)
...@@ -176,7 +176,7 @@ class Test_TestResult(unittest.TestCase): ...@@ -176,7 +176,7 @@ class Test_TestResult(unittest.TestCase):
self.assertEqual(result.shouldStop, False) self.assertEqual(result.shouldStop, False)
test_case, formatted_exc = result.failures[0] test_case, formatted_exc = result.failures[0]
self.assertTrue(test_case is test) self.assertIs(test_case, test)
self.assertIsInstance(formatted_exc, str) self.assertIsInstance(formatted_exc, str)
# "addError(test, err)" # "addError(test, err)"
...@@ -224,7 +224,7 @@ class Test_TestResult(unittest.TestCase): ...@@ -224,7 +224,7 @@ class Test_TestResult(unittest.TestCase):
self.assertEqual(result.shouldStop, False) self.assertEqual(result.shouldStop, False)
test_case, formatted_exc = result.errors[0] test_case, formatted_exc = result.errors[0]
self.assertTrue(test_case is test) self.assertIs(test_case, test)
self.assertIsInstance(formatted_exc, str) self.assertIsInstance(formatted_exc, str)
def testGetDescriptionWithoutDocstring(self): def testGetDescriptionWithoutDocstring(self):
......
...@@ -177,7 +177,7 @@ class CallTest(unittest.TestCase): ...@@ -177,7 +177,7 @@ class CallTest(unittest.TestCase):
args = _Call(((1, 2, 3), {})) args = _Call(((1, 2, 3), {}))
self.assertEqual(args, call(1, 2, 3)) self.assertEqual(args, call(1, 2, 3))
self.assertEqual(call(1, 2, 3), args) self.assertEqual(call(1, 2, 3), args)
self.assertTrue(call(1, 2, 3) in [args]) self.assertIn(call(1, 2, 3), [args])
def test_call_ne(self): def test_call_ne(self):
...@@ -793,7 +793,7 @@ class SpecSignatureTest(unittest.TestCase): ...@@ -793,7 +793,7 @@ class SpecSignatureTest(unittest.TestCase):
mock_property = foo.foo mock_property = foo.foo
# no spec on properties # no spec on properties
self.assertTrue(isinstance(mock_property, MagicMock)) self.assertIsInstance(mock_property, MagicMock)
mock_property(1, 2, 3) mock_property(1, 2, 3)
mock_property.abc(4, 5, 6) mock_property.abc(4, 5, 6)
mock_property.assert_called_once_with(1, 2, 3) mock_property.assert_called_once_with(1, 2, 3)
...@@ -826,19 +826,19 @@ class TestCallList(unittest.TestCase): ...@@ -826,19 +826,19 @@ class TestCallList(unittest.TestCase):
mock(b=6) mock(b=6)
for kall in call(1, 2), call(a=3), call(3, 4), call(b=6): for kall in call(1, 2), call(a=3), call(3, 4), call(b=6):
self.assertTrue(kall in mock.call_args_list) self.assertIn(kall, mock.call_args_list)
calls = [call(a=3), call(3, 4)] calls = [call(a=3), call(3, 4)]
self.assertTrue(calls in mock.call_args_list) self.assertIn(calls, mock.call_args_list)
calls = [call(1, 2), call(a=3)] calls = [call(1, 2), call(a=3)]
self.assertTrue(calls in mock.call_args_list) self.assertIn(calls, mock.call_args_list)
calls = [call(3, 4), call(b=6)] calls = [call(3, 4), call(b=6)]
self.assertTrue(calls in mock.call_args_list) self.assertIn(calls, mock.call_args_list)
calls = [call(3, 4)] calls = [call(3, 4)]
self.assertTrue(calls in mock.call_args_list) self.assertIn(calls, mock.call_args_list)
self.assertFalse(call('fish') in mock.call_args_list) self.assertNotIn(call('fish'), mock.call_args_list)
self.assertFalse([call('fish')] in mock.call_args_list) self.assertNotIn([call('fish')], mock.call_args_list)
def test_call_list_str(self): def test_call_list_str(self):
......
...@@ -37,12 +37,12 @@ class TestMockingMagicMethods(unittest.TestCase): ...@@ -37,12 +37,12 @@ class TestMockingMagicMethods(unittest.TestCase):
return self, 'fish' return self, 'fish'
mock.__getitem__ = f mock.__getitem__ = f
self.assertFalse(mock.__getitem__ is f) self.assertIsNot(mock.__getitem__, f)
self.assertEqual(mock['foo'], (mock, 'fish')) self.assertEqual(mock['foo'], (mock, 'fish'))
self.assertEqual(mock.__getitem__('foo'), (mock, 'fish')) self.assertEqual(mock.__getitem__('foo'), (mock, 'fish'))
mock.__getitem__ = mock mock.__getitem__ = mock
self.assertTrue(mock.__getitem__ is mock) self.assertIs(mock.__getitem__, mock)
def test_magic_methods_isolated_between_mocks(self): def test_magic_methods_isolated_between_mocks(self):
...@@ -212,8 +212,8 @@ class TestMockingMagicMethods(unittest.TestCase): ...@@ -212,8 +212,8 @@ class TestMockingMagicMethods(unittest.TestCase):
self.assertEqual(len(mock), 6) self.assertEqual(len(mock), 6)
mock.__contains__ = lambda s, o: o == 3 mock.__contains__ = lambda s, o: o == 3
self.assertTrue(3 in mock) self.assertIn(3, mock)
self.assertFalse(6 in mock) self.assertNotIn(6, mock)
mock.__iter__ = lambda s: iter('foobarbaz') mock.__iter__ = lambda s: iter('foobarbaz')
self.assertEqual(list(mock), list('foobarbaz')) self.assertEqual(list(mock), list('foobarbaz'))
......
...@@ -52,7 +52,7 @@ class MockTest(unittest.TestCase): ...@@ -52,7 +52,7 @@ class MockTest(unittest.TestCase):
"method_calls not initialised correctly") "method_calls not initialised correctly")
# Can't use hasattr for this test as it always returns True on a mock # Can't use hasattr for this test as it always returns True on a mock
self.assertFalse('_items' in mock.__dict__, self.assertNotIn('_items', mock.__dict__,
"default mock should not have '_items' attribute") "default mock should not have '_items' attribute")
self.assertIsNone(mock._mock_parent, self.assertIsNone(mock._mock_parent,
...@@ -493,19 +493,19 @@ class MockTest(unittest.TestCase): ...@@ -493,19 +493,19 @@ class MockTest(unittest.TestCase):
pass pass
mock = Mock(spec=X) mock = Mock(spec=X)
self.assertTrue(isinstance(mock, X)) self.assertIsInstance(mock, X)
mock = Mock(spec=X()) mock = Mock(spec=X())
self.assertTrue(isinstance(mock, X)) self.assertIsInstance(mock, X)
self.assertIs(mock.__class__, X) self.assertIs(mock.__class__, X)
self.assertEqual(Mock().__class__.__name__, 'Mock') self.assertEqual(Mock().__class__.__name__, 'Mock')
mock = Mock(spec_set=X) mock = Mock(spec_set=X)
self.assertTrue(isinstance(mock, X)) self.assertIsInstance(mock, X)
mock = Mock(spec_set=X()) mock = Mock(spec_set=X())
self.assertTrue(isinstance(mock, X)) self.assertIsInstance(mock, X)
def test_setting_attribute_with_spec_set(self): def test_setting_attribute_with_spec_set(self):
......
...@@ -17,7 +17,7 @@ class SentinelTest(unittest.TestCase): ...@@ -17,7 +17,7 @@ class SentinelTest(unittest.TestCase):
def testDEFAULT(self): def testDEFAULT(self):
self.assertTrue(DEFAULT is sentinel.DEFAULT) self.assertIs(DEFAULT, sentinel.DEFAULT)
def testBases(self): def testBases(self):
# If this doesn't raise an AttributeError then help(mock) is broken # If this doesn't raise an AttributeError then help(mock) is broken
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment