Skip to content

Commit a8a7267

Browse files
authored
Fix and improve .assertValidHTML() test method (django-commons#1597)
* Make .assertValidHTML() parse passed content Prior to this commit, the BaseTestCase.assertValidHTML() method was not parsing the content from the provided argument, but was instead parsing self.panel.content. For most usages, the passed in content was the same as self.panel.content, but for the TemplatesPanelTestCase.test_template_repr() test case it was not, resulting in an invalid test. * Fix test failure exposed by previous commit * Remove unused .assertValidHTML() msg argument Allows removal of a call to the internal TestCase._formatMessage() method. * Tweak .assertValidHTML() exception message Since the passed in HTML does not necessarily represent the panel content, change the message to no longer reference "Content".
1 parent 6a70876 commit a8a7267

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

tests/base.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,16 @@ def tearDown(self):
3131
def get_response(self, request):
3232
return self._get_response(request)
3333

34-
def assertValidHTML(self, content, msg=None):
34+
def assertValidHTML(self, content):
3535
parser = html5lib.HTMLParser()
36-
parser.parseFragment(self.panel.content)
36+
parser.parseFragment(content)
3737
if parser.errors:
38-
default_msg = ["Content is invalid HTML:"]
38+
msg_parts = ["Invalid HTML:"]
3939
lines = content.split("\n")
4040
for position, errorcode, datavars in parser.errors:
41-
default_msg.append(" %s" % html5lib.constants.E[errorcode] % datavars)
42-
default_msg.append(" %s" % lines[position[0] - 1])
43-
44-
msg = self._formatMessage(msg, "\n".join(default_msg))
45-
raise self.failureException(msg)
41+
msg_parts.append(" %s" % html5lib.constants.E[errorcode] % datavars)
42+
msg_parts.append(" %s" % lines[position[0] - 1])
43+
raise self.failureException("\n".join(msg_parts))
4644

4745

4846
class IntegrationTestCase(TestCase):

tests/panels/test_template.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_template_repr(self):
4747

4848
User.objects.create(username="admin")
4949
bad_repr = TemplateReprForm()
50-
t = Template("{{ bad_repr }}")
50+
t = Template("<table>{{ bad_repr }}</table>")
5151
c = Context({"bad_repr": bad_repr})
5252
html = t.render(c)
5353
self.assertIsNotNone(html)

0 commit comments

Comments
 (0)