Commit 7558d57a authored by Gregory P. Smith's avatar Gregory P. Smith

Rename the actual method definitions to the official assertFoo names.

Adds unittests to make sure the old fail* names continue to work now
and adds a comment that they are pending deprecation.

Also adds a test to confirm that the plural Equals method variants
continue to exist even though we're unlikely to deprecate those.

http://bugs.python.org/issue2578
parent 6eabc244
...@@ -2283,7 +2283,6 @@ class Test_TestCase(TestCase, TestEquality, TestHashing): ...@@ -2283,7 +2283,6 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
'Tests shortDescription() for a method with a longer ' 'Tests shortDescription() for a method with a longer '
'docstring.')) 'docstring.'))
def testAddTypeEqualityFunc(self): def testAddTypeEqualityFunc(self):
class SadSnake(object): class SadSnake(object):
"""Dummy class for test_addTypeEqualityFunc.""" """Dummy class for test_addTypeEqualityFunc."""
...@@ -2658,6 +2657,33 @@ test case ...@@ -2658,6 +2657,33 @@ test case
self.assertRaisesRegexp, Exception, self.assertRaisesRegexp, Exception,
re.compile('^Expected$'), Stub) re.compile('^Expected$'), Stub)
def testSynonymAssertMethodNames(self):
"""Test undocumented method name synonyms.
Please do not use these methods names in your own code.
This test confirms their continued existence and functionality
in order to avoid breaking existing code.
"""
self.assertNotEquals(3, 5)
self.assertEquals(3, 3)
self.assertAlmostEquals(2.0, 2.0)
self.assertNotAlmostEquals(3.0, 5.0)
self.assert_(True)
def testPendingDeprecationMethodNames(self):
"""Test fail* methods pending deprecation, they will warn in 3.2.
Do not use these methods. They will go away in 3.3.
"""
self.failIfEqual(3, 5)
self.failUnlessEqual(3, 3)
self.failUnlessAlmostEqual(2.0, 2.0)
self.failIfAlmostEqual(3.0, 5.0)
self.failUnless(True)
self.failUnlessRaises(TypeError, lambda _: 3.14 + u'spam')
self.failIf(False)
class Test_TestSkipping(TestCase): class Test_TestSkipping(TestCase):
......
...@@ -467,17 +467,17 @@ class TestCase(object): ...@@ -467,17 +467,17 @@ class TestCase(object):
"""Fail immediately, with the given message.""" """Fail immediately, with the given message."""
raise self.failureException(msg) raise self.failureException(msg)
def failIf(self, expr, msg=None): def assertFalse(self, expr, msg=None):
"Fail the test if the expression is true." "Fail the test if the expression is true."
if expr: if expr:
raise self.failureException(msg) raise self.failureException(msg)
def failUnless(self, expr, msg=None): def assertTrue(self, expr, msg=None):
"""Fail the test unless the expression is true.""" """Fail the test unless the expression is true."""
if not expr: if not expr:
raise self.failureException(msg) raise self.failureException(msg)
def failUnlessRaises(self, excClass, callableObj=None, *args, **kwargs): def assertRaises(self, excClass, callableObj=None, *args, **kwargs):
"""Fail unless an exception of class excClass is thrown """Fail unless an exception of class excClass is thrown
by callableObj when invoked with arguments args and keyword by callableObj when invoked with arguments args and keyword
arguments kwargs. If a different type of exception is arguments kwargs. If a different type of exception is
...@@ -488,7 +488,7 @@ class TestCase(object): ...@@ -488,7 +488,7 @@ class TestCase(object):
If called with callableObj omitted or None, will return a If called with callableObj omitted or None, will return a
context object used like this:: context object used like this::
with self.failUnlessRaises(some_error_class): with self.assertRaises(some_error_class):
do_something() do_something()
""" """
context = _AssertRaisesContext(excClass, self) context = _AssertRaisesContext(excClass, self)
...@@ -524,21 +524,21 @@ class TestCase(object): ...@@ -524,21 +524,21 @@ class TestCase(object):
if not first == second: if not first == second:
raise self.failureException(msg or '%r != %r' % (first, second)) raise self.failureException(msg or '%r != %r' % (first, second))
def failUnlessEqual(self, first, second, msg=None): def assertEqual(self, first, second, msg=None):
"""Fail if the two objects are unequal as determined by the '==' """Fail if the two objects are unequal as determined by the '=='
operator. operator.
""" """
assertion_func = self._getAssertEqualityFunc(first, second) assertion_func = self._getAssertEqualityFunc(first, second)
assertion_func(first, second, msg=msg) assertion_func(first, second, msg=msg)
def failIfEqual(self, first, second, msg=None): def assertNotEqual(self, first, second, msg=None):
"""Fail if the two objects are equal as determined by the '==' """Fail if the two objects are equal as determined by the '=='
operator. operator.
""" """
if first == second: if first == second:
raise self.failureException(msg or '%r == %r' % (first, second)) raise self.failureException(msg or '%r == %r' % (first, second))
def failUnlessAlmostEqual(self, first, second, places=7, msg=None): def assertAlmostEqual(self, first, second, places=7, msg=None):
"""Fail if the two objects are unequal as determined by their """Fail if the two objects are unequal as determined by their
difference rounded to the given number of decimal places difference rounded to the given number of decimal places
(default 7) and comparing to zero. (default 7) and comparing to zero.
...@@ -550,7 +550,7 @@ class TestCase(object): ...@@ -550,7 +550,7 @@ class TestCase(object):
raise self.failureException( raise self.failureException(
msg or '%r != %r within %r places' % (first, second, places)) msg or '%r != %r within %r places' % (first, second, places))
def failIfAlmostEqual(self, first, second, places=7, msg=None): def assertNotAlmostEqual(self, first, second, places=7, msg=None):
"""Fail if the two objects are equal as determined by their """Fail if the two objects are equal as determined by their
difference rounded to the given number of decimal places difference rounded to the given number of decimal places
(default 7) and comparing to zero. (default 7) and comparing to zero.
...@@ -564,19 +564,24 @@ class TestCase(object): ...@@ -564,19 +564,24 @@ class TestCase(object):
# Synonyms for assertion methods # Synonyms for assertion methods
assertEqual = assertEquals = failUnlessEqual # The plurals are undocumented. Keep them that way to discourage use.
# Do not add more. Do not remove.
assertNotEqual = assertNotEquals = failIfEqual # Going through a deprecation cycle on these would annoy many people.
assertEquals = assertEqual
assertAlmostEqual = assertAlmostEquals = failUnlessAlmostEqual assertNotEquals = assertNotEqual
assertAlmostEquals = assertAlmostEqual
assertNotAlmostEqual = assertNotAlmostEquals = failIfAlmostEqual assertNotAlmostEquals = assertNotAlmostEqual
assert_ = assertTrue
assertRaises = failUnlessRaises
# These fail* assertion method names are pending deprecation and will
assert_ = assertTrue = failUnless # be a deprecation warning in 3.2; http://bugs.python.org/issue2578
failUnlessEqual = assertEqual
assertFalse = failIf failIfEqual = assertNotEqual
failUnlessAlmostEqual = assertAlmostEqual
failIfAlmostEqual = assertNotAlmostEqual
failUnless = assertTrue
failUnlessRaises = assertRaises
failIf = assertFalse
def assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None): def assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None):
......
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