11

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????
Calvin Cheng
  • 35,640
  • 39
  • 116
  • 167
  • 3
    When you read the documentation, what exact words confused you? Did you read about the `==` and the `is` operators? When you read the difference between the `==` and the `is` operator what exact words confused you? It helps if you provide quotes of the material you read so that we target the answer to the words that confused you. – S.Lott Sep 02 '11 at 09:58
  • 1
    possible duplicate of [What is the semantics of 'is' operator in Python?](http://stackoverflow.com/questions/2438667/what-is-the-semantics-of-is-operator-in-python) – S.Lott Sep 02 '11 at 10:14
  • This answer will help you http://stackoverflow.com/questions/1504717/python-vs-is-comparing-strings-is-fails-sometimes-why/1504742#1504742 – Bobby Sep 02 '11 at 10:19

1 Answers1

24

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.

Matthew Rankin
  • 457,139
  • 39
  • 126
  • 163
  • The inequality comparison changed in 3.x versions: Otherwise, the == and != operators always consider objects of different types to be unequal, while the <, >, >= and <= operators raise a TypeError when comparing objects of different types that do not implement these operators for the given pair of types. – Xavier Combelle Sep 02 '11 at 11:06