Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#21480 closed Bug (invalid)

assertContains(…, html=True) does not work as expected

Reported by: Jannis Vajen Owned by: nobody
Component: Testing framework Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Excuse me if I'm opening a bug report for an intended feature right now, but I'm surprised that SimpleTestClient.assertContains(…, html=True) does not find strings when they're present.

    def test_no_tags(self):
        response = self.client.get("/view/?content=this is a response")
        self.assertContains(response, "response")

    def test_no_tags_html(self):
        response = self.client.get("/view/?content=this is a response")
        self.assertContains(response, "response", html=True)

    def test_tag_in_response(self):
        response = self.client.get("/view/?content=this is a <span>response</span>")
        self.assertContains(response, "response")

    def test_tag_in_response_html(self):
        response = self.client.get("/view/?content=this is a <span>response</span>")
        self.assertContains(response, "response", html=True)

    def test_tag_in_needle_and_reponse(self):
        response = self.client.get("/view/?content=this is a <span>response</span>")
        self.assertContains(response, "<span>response</span>")

    def test_tag_in_needle_and_reponse_html(self):
        response = self.client.get("/view/?content=this is a <span> response</span>")
        self.assertContains(response, "<span>response</span>", html=True)
.F...F
======================================================================
FAIL: test_no_tags_html
----------------------------------------------------------------------
Traceback (most recent call last):
 in test_no_tags_html
    self.assertContains(response, "response", html=True)
  File "django/test/testcases.py", line 664, in assertContains
    msg_prefix + "Couldn't find %s in response" % text_repr)
AssertionError: Couldn't find 'response' in response

======================================================================
FAIL: test_tag_in_response_html
----------------------------------------------------------------------
Traceback (most recent call last):
  in test_tag_in_response_html
    self.assertContains(response, "response", html=True)
  File "django/test/testcases.py", line 664, in assertContains
    msg_prefix + "Couldn't find %s in response" % text_repr)
AssertionError: Couldn't find 'response' in response

----------------------------------------------------------------------
Ran 6 tests in 0.026s

FAILED (failures=2)

It seems that the argument text in TestCase.assertContains(response, text, count=None, status_code=200, msg_prefix='', html=False) needs to have tags present to match which is not always the case although whitespace (especially newlines) should be ignored.

Change History (6)

comment:1 by Claude Paroz, 10 years ago

Could you please tell us which Python version you're using?

comment:2 by Jannis Vajen, 10 years ago

Tested with Python 2.7.4, Django 1.6 and Django 1.5.5

comment:3 by Claude Paroz, 10 years ago

I've investigated and yes, the behavior you describe is expected with current code. With html=True, you are supposed to look for HTML nodes. When you provide content with no tags, the whole content is considered a node, hence (taking span as just an illustration) <span>response</span> is not found in <span>This is a response</span>, that's the current logic.
We could try to special case when provided values contain no tags, but we should have a real use case to demonstrate its utility. In the examples above, you should not set html=True if the provided comparison value is not an HTML expression.

comment:4 by Claude Paroz, 10 years ago

Resolution: invalid
Status: newclosed

I think the documentation is pretty clear:

Set ``html`` to ``True`` to handle ``text`` as HTML. The comparison with
the response content will be based on HTML semantics instead of
character-by-character equality.

https://docs.djangoproject.com/en/dev/topics/testing/overview/#django.test.SimpleTestCase.assertContains

comment:5 by Jannis Vajen, 10 years ago

Ok, thanks for clarifying. The reason why I started investigating is that I want to test for the presence of a message that is broken into several lines due to PEP8 compliance. In the HTML output there's lots of whitespace where newlines are in the source.

Thanks again.

comment:6 by Claude Paroz, 10 years ago

I see. You might also experiment with asserting using assertRegexpMatches on response.content (and \s* for spaces), but YMMV.

Note: See TracTickets for help on using tickets.
Back to Top