Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between assertEqual and assertIs (assertIs was introduced in Python 2.7)?

Reference - http://docs.python.org/library/unittest.html#assert-methods

assertEqual(a, b)   # checks that a == b
assertIs(a, b)  # checks that a is b  <---- whatever that means????
like image 862
Calvin Cheng Avatar asked Sep 02 '11 09:09

Calvin Cheng


1 Answers

Using assertEqual the two objects need not be of the same type, they merely need to be the same value. In comparison, using assertIs the two objects need to be the same object.

assertEqual tests for equality like the == operator:

The operators <, >, ==, >=, <=, and != compare the values of two objects. The objects need not have the same type. If both are numbers, they are converted to a common type. Otherwise, objects of different types always compare unequal, and are ordered consistently but arbitrarily.

assertIs test for object identity same as the is and is not operators:

The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.

The above quotes both come from the Python documentation section 5.9 Comparisons.

like image 86
Matthew Rankin Avatar answered Oct 15 '22 02:10

Matthew Rankin