Django error: assertInHTML ('hello', '<html> hello </html>)

In the Django shell:

from django.test import SimpleTestCase
c = SimpleTestCase()
haystack = '<html><b>contribution</b></html>' 

c.assertInHTML('<b>contribution</b>', haystack)
c.assertInHTML('contribution', haystack)

I do not understand why the first statement passes, but the second does not:

AssertionError                            Traceback (most recent call last)
<ipython-input-15-20da22474686> in <module>()
      5 
      6 c.assertInHTML('<b>contribution</b>', haystack)
----> 7 c.assertInHTML('contribution', haystack)

c:\...\lib\site-packages\django\test\testcases.py in assertInHTML(self, needle, haystack, count, msg_prefix)
    680         else:
    681             self.assertTrue(real_count != 0,
--> 682                 msg_prefix + "Couldn't find '%s' in response" % needle)
    683 
    684     def assertJSONEqual(self, raw, expected_data, msg=None):

C:\...\Programs\Python\Python35-32\lib\unittest\case.py in assertTrue(self, expr, msg)
    675         if not expr:
    676             msg = self._formatMessage(msg, "%s is not true" % safe_repr(expr))
--> 677             raise self.failureException(msg)
    678 
    679     def _formatMessage(self, msg, standardMsg):

AssertionError: False is not true : Couldn't find 'contribution' in response

Django docs will simply say: "The arguments passed must be valid HTML." I do not think this is a problem because the call assert_and_parse_htmlin the first line does not rise:

def assertInHTML(self, needle, haystack, count=None, msg_prefix=''):
    needle = assert_and_parse_html(self, needle, None,
        'First argument is not valid HTML:')
    haystack = assert_and_parse_html(self, haystack, None,
        'Second argument is not valid HTML:')
    real_count = haystack.count(needle)
    if count is not None:
        self.assertEqual(real_count, count,
            msg_prefix + "Found %d instances of '%s' in response"
            " (expected %d)" % (real_count, needle, count))
    else:
        self.assertTrue(real_count != 0,
            msg_prefix + "Couldn't find '%s' in response" % needle)

I am using Python 3.5.1 and Django 1.8.8.

+4
source share
1 answer

This is a bug in Django :

assertInHTML(needle, haystack) has the following behavior

assertInHTML('<p>a</p>', '<div><p>a</p><p>b</p></div>') passes: clearly correct

assertInHTML('<p>a</p><p>b</p>', '<p>a</p><p>b</p>') passes: perhaps correct

assertInHTML('<p>a</p><p>b</p>', '<div><p>a</p><p>b</p></div>') not executed with an approval error.

, , .

( !) , , , HTML-, .

+3

Source: https://habr.com/ru/post/1650380/


All Articles