Python unittest断言错误:unicode字符串不是unicode字符串

Python unittest断言错误:unicode字符串不是unicode字符串,python,python-2.7,unit-testing,selenium,Python,Python 2.7,Unit Testing,Selenium,我有一个web服务器,它返回包含以下内容的HTML: <div class="well"> <blockquote> <h2>Blueberry Pancakes Are Bomb</h2> </blockquote> </div> 运行测试时,出现以下错误: (foodie_env)fatman:foodie$ python functional_tests.py .F =========

我有一个web服务器,它返回包含以下内容的HTML:

<div class="well">
    <blockquote>
        <h2>Blueberry Pancakes Are Bomb</h2>
    </blockquote>
</div>
运行测试时,出现以下错误:

(foodie_env)fatman:foodie$ python functional_tests.py
.F
======================================================================
FAIL: test_page_has_blueberry_in_blockquote (__main__.NewVisitorNavbar)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "functional_tests.py", line 179, in test_page_has_blueberry_in_blockquote
    self.assertIs(food_text, u'Blueberry Pancakes Are Bomb')
AssertionError: u'Blueberry Pancakes Are Bomb' is not u'Blueberry Pancakes Are Bomb'

----------------------------------------------------------------------
Ran 2 tests in 6.656s

FAILED (failures=1)
我也试过:

self.assertIs(food_text, 'Blueberry Pancakes Are Bomb')
将字符串转换为unicode或非unicode似乎不会改变任何东西。我仍然得到相同的断言错误

更新:如果我将断言测试更改为:

self.assertEquals(food_text, u'Blueberry Pancakes Are Bomb')
但是,我仍然想知道为什么
assertIs()
测试失败。我猜这是由于字符串在内存中的表示方式。直观地说,assertIs()版本应该通过,因为我正在比较两种字符串类型


断言错误不是很直观,令人困惑。是什么导致了这种奇怪的断言错误?

尝试将断言替换为:

self.assertEqual(food_text, u'Blueberry Pancakes Are Bomb')

为了进一步阐述@TomKarzes的答案,
assertIs()
正在评估两个对象。在以下代码中,这两个字符串表示为内存中的两个不同对象:

self.assertEqual(food_text, u'Blueberry Pancakes Are Bomb')
因此,该断言将失败,因为这两个对象的计算结果不相同


另一方面,
assertEquals()
正在比较两个字符串的相似性,因此此断言通过。

是否需要将第二个参数强制为unicode?@dursk不需要,问题仍然存在。我尝试了这两种方法,结果都是一样的。为什么要使用
assertIs
而不是
assertEqual
?我认为unicode字符串不能保证哈希唯一性,所以您可能应该比较使用
==
而不是
is
。基本上,同一unicode字符串有两个单独的cope,尽管它们相等,但它们指的是同一文本的单独副本。请查看并搜索
断言。基本上,它检查
a是否为b
,而实际上您需要
assertEqual
,它检查
a==b
self.assertEqual(food_text, u'Blueberry Pancakes Are Bomb')