diff --git a/django/test/html.py b/django/test/html.py index cca4a48491bc9..4a8a5561e9234 100644 --- a/django/test/html.py +++ b/django/test/html.py @@ -95,6 +95,9 @@ def _count(self, element, count=True): if not isinstance(element, six.string_types): if self == element: return 1 + if isinstance(element, RootElement): + raise ValueError('Needle HTML does not have a root element') + i = 0 for child in self.children: # child is text content and element is also text content, then diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py index 0906291d0b768..a46ee63845023 100644 --- a/tests/test_utils/tests.py +++ b/tests/test_utils/tests.py @@ -576,7 +576,8 @@ def test_html_contain(self): # when a root element is used for the needle but not the haystack dom1 = parse_html('

foo

bar

') dom2 = parse_html('

foo

bar

') - self.assertIn(dom1, dom2) + with self.assertRaises(ValueError): + self.assertIn(dom1, dom2) def test_count(self): # equal html contains each other one time