我有一个web服务器,它返回包含以下内容的HTML:
<div class="well">
<blockquote>
<h2>Blueberry Pancakes Are Bomb</h2>
</blockquote>
</div>
我写了一个做作的功能测试:
^{pr2}$运行测试时,出现以下错误:
(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似乎不会改变任何内容。我仍然得到同样的断言错误。在
更新:如果将断言测试更改为:
self.assertEquals(food_text, u'Blueberry Pancakes Are Bomb')
但是,我仍然想知道assertIs()
测试失败的原因。我猜这是由于字符串在内存中是如何表示的。直观地说,assertions()版本应该通过,因为我比较了两个字符串类型。在
断言错误不是很直观,容易混淆。是什么导致了这个奇怪的断言错误?在
为了进一步扩展@TomKarzes的答案,
assertIs()
正在计算两个对象。在以下代码中,这两个字符串在内存中表示为两个不同的对象:因此,这个断言将失败,因为这两个对象的计算结果并不相同。在
另一方面,
assertEquals()
正在比较两个字符串的相似性,因此该断言通过。在尝试将
assertIs
检查替换为:相关问题 更多 >
编程相关推荐