Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assertEquals vs. assertEqual in python

People also ask

What is Assertequals in Python?

assertEqual() in Python is a unittest library function that is used in unit testing to check the equality of two values. This function will take three parameters as input and return a boolean value depending upon the assert condition. If both input values are equal assertEqual() will return true else return false.

What does assertTrue do in Python?

assertTrue() in Python is a unittest library function that is used in unit testing to compare test value with true. This function will take two parameters as input and return a boolean value depending upon the assert condition. If test value is true then assertTrue() will return true else return false.


Actually, in Python 2.6, both assertEqual and assertEquals are convenience aliases to failUnlessEqual. The source declares them thus:

# Synonyms for assertion methods
assertEqual = assertEquals = failUnlessEqual

In Python 3, to your point, failUnlessEqual is explicitly deprecated. assertEquals carries this comment :-)

# Synonyms for assertion methods
# The plurals are undocumented.  Keep them that way to discourage use.
# Do not add more.  Do not remove.
# Going through a deprecation cycle on these would annoy many people.

So, the upshot appears to be that you should use whatever you like for Python 2.x, but tend toward assertEqual for Python 3.


A 3.3 update: From 26.3.7.1.1. Deprecated aliases :

For historical reasons, some of the TestCase methods had one or more aliases that are now deprecated. The following table lists the correct names along with their deprecated aliases:

Method Name   | Deprecated alias | Deprecated alias
--------------+------------------+-----------------
assertEqual() | failUnlessEqual  | assertEquals
...

Not just for Python 3.x, since Python 2.7 assertEquals has been deprecated as well:

Method Name            | Deprecated alias(es)
_________________________________________________________
assertEqual()          | failUnlessEqual, assertEquals

From 25.3.7.1.1. Deprecated aliases


I think this was tension between the "only one obvious way to do it" vs. "alias to make the overall code flow semantically". Personally I found I like to read

failIf(some_condition)

over

assertFalse(some_condition)

but liked

assertEqual(a, b)

over the other two (assertEquals(a, b) bothers my sense of grammar).

The "only one obvious way to do it" has taken precedence going forward.


I don't find any mention of assertEquals in http://docs.python.org/library/unittest.html. However, when I import TestCase and then do a "help(TestCase)", it's listed. I think it's just a synonym for convenience.


I know it doesn't answer the specific question, but if you got here while searching for:

using deprecated method assertEquals()

You just need to change the call to .assertEqual() (remove the 's' in equalS)