[Solved]-Django assert failure: assertInHTML('hello', '<html>hello</html>')

12👍

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.

Leave a comment