Reputation: 4402
I have a web server that returns HTML containing the following:
<div class="well">
<blockquote>
<h2>Blueberry Pancakes Are Bomb</h2>
</blockquote>
</div>
I wrote a contrived functional test like so:
def test_page_has_blueberry_in_blockquote(self):
# User goes to inspire_my_palate page
self.browser.get('http://localhost:8000/inspire_my_palate')
# He sees a blockquote with a header containing 'Blueberry ...'
food_text = self.browser.find_element_by_xpath('//div[@class="well"]/blockquote/h2').text
self.assertIs(food_text, u'Blueberry Pancakes Are Bomb')
When I run the test, I get this error:
(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)
I've also tried:
self.assertIs(food_text, 'Blueberry Pancakes Are Bomb')
Casting the string as unicode or not doesn't appear to change anything. I still get the same assertion error.
Update: I get a passing test if I change the assertion test to:
self.assertEquals(food_text, u'Blueberry Pancakes Are Bomb')
However, I'd still still like to know why the assertIs()
test fails. I'm guessing this is due to how the strings are represented in memory. Intuitively, the assertIs() version should pass since I'm comparing two string types.
The assertion error is not very intuitive and is confusing. What could be causing this weird assertion error?
Upvotes: 0
Views: 3722
Reputation: 4402
To expand more on @TomKarzes's answer, assertIs()
is evaluating two objects. In the following code, the two strings are represented as two different objects in memory:
self.assertEqual(food_text, u'Blueberry Pancakes Are Bomb')
Therefore, this assertion will fail, since these two objects do not evaluate to the same thing.
On the other hand, assertEquals()
is comparing the two strings for likeness, therefore this assertion passes.
Upvotes: 0
Reputation: 24052
Try replacing the assertIs
check with:
self.assertEqual(food_text, u'Blueberry Pancakes Are Bomb')
Upvotes: 2