In 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 don't understand why the first assertion passes, but the second one doesn't:
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
The Django docs just say "The passed-in arguments must be valid HTML." I don't think that is the problem, because the call to assert_and_parse_html on the first line doesn't raise:
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'm using Python 3.5.1 and Django 1.8.8.
This is a bug in Django:
assertInHTML(needle, haystack)has the following behaviour
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: possibly correct
assertInHTML('<p>a</p><p>b</p>', '<div><p>a</p><p>b</p></div>')fails with an assertion error.
The problem occurs when the needle does not have a unique root element that wraps everything else.
The proposed fix (which has been languishing for some time!) is to raise an exception if you try to do this - i.e., the needle must have a HTML tag that wraps everything inside it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With