assertEqual和断言(断言是在Python2.7中引入的)有什么区别?

2024-04-28 11:19:48 发布

您现在位置:Python中文网/ 问答频道 /正文

参考-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????

Tags: orghttpdocsthatishtmllibraryassert
1条回答
网友
1楼 · 发布于 2024-04-28 11:19:48

使用assertEqual两个对象不必是同一类型,它们只需要是相同的值。相比之下,使用assertIs时,两个对象需要是相同的对象。

assertEqual类似于==运算符的相等性测试:

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测试与isis not运算符相同的对象标识:

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.

以上两个引号都来自Python文档部分5.9 Comparisons

相关问题 更多 >