#24112: Inconsistency in TestCase.assertInHTML
-----------------------------------+--------------------------------------
Reporter: plumdog | Owner: nobody
Type: Uncategorized | Status: new
Component: Testing framework | Version: 1.7
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by plumdog):
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Old description:
> 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:
> {{{
> File ".../django/test/testcases.py", line 673, in assertInHTML
> msg_prefix + "Couldn't find '%s' in response" % needle)
> AssertionError: Couldn't find '<p>
> a
> </p><p>
> b
> </p>' in response
> }}}
>
> Which seems wrong. It doesn't handle the case correctly if the needle
> doesn't have a single top-level element (unless the two bits of html are
> equivalent). This is down to the `_count` method of
> `django.test.html.Element`
>
> I think this should throw a ValueError if the needle has multiple top
> level elements. However, this does change existing behaviour for the case
> where the html fragments are equivalent. An alternative would be to try
> to correctly handle the count in this case, which is possibly a bit
> fiddly, and the current implementation appears to make no attempt to do
> this.
>
> I have a patch for the first case:
> https://github.com/plumdog/django/commit/bfdfda315ad74d067be52888a236ab7a4aadcf96
New description:
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:
{{{
File ".../django/test/testcases.py", line 673, in assertInHTML
msg_prefix + "Couldn't find '%s' in response" % needle)
AssertionError: Couldn't find '<p>
a
</p><p>
b
</p>' in response
}}}
Which seems wrong. It doesn't handle the case correctly if the needle
doesn't have a single top-level element (unless the two bits of html are
equivalent). This is down to the `_count` method of
`django.test.html.Element`.
This means that the test will fail (with the error above) rather than
erroring.
I think this should throw a ValueError if the needle has multiple top
level elements. However, this does change existing behaviour for the case
where the html fragments are equivalent. An alternative would be to try to
correctly handle the count in this case, which is possibly a bit fiddly,
and the current implementation appears to make no attempt to do this.
I have a patch for the first case:
https://github.com/plumdog/django/commit/bfdfda315ad74d067be52888a236ab7a4aadcf96
--
--
Ticket URL: <https://code.djangoproject.com/ticket/24112#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.b3ca79e92117c16e38756b3f6208856a%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.