[Django] #31378: django.test.utils compare_xml assumes string, errors on bytes

17 views
Skip to first unread message

Django

unread,
Mar 18, 2020, 12:47:30 PM3/18/20
to django-...@googlegroups.com
#31378: django.test.utils compare_xml assumes string, errors on bytes
---------------------------------------------+------------------------
Reporter: Joe Germuska | Owner: nobody
Type: Uncategorized | Status: new
Component: Testing framework | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------------+------------------------
In writing a test case, I found that passing the test client's
`response.content` object into `TestCase.assertXMLEqual()` resulted in
this error: `a bytes-like object is required, not 'str'`

In tracing the code, this originates from
[https://github.com/django/django/blob/ba4389a36b5fb1afce0cddb4e28233138b6612b7/django/test/utils.py#L586-L587
django.test.utils.compare_xml], where string literals `\\n` and `\n` are
passed to `replace()` even though the types of `want` and `got`
(particularly `got`) may be `bytes` and not `str`.

The workaround is to decode the response content
(`response.content.decode('utf-8')`) and to pass the assertion comparison
("want") also as a string, but if this method is going to be used in the
testing framework, it seems that it should tolerate directly passing in
`response.content`, even if that's a `bytes`.

--
Ticket URL: <https://code.djangoproject.com/ticket/31378>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 18, 2020, 12:50:13 PM3/18/20
to django-...@googlegroups.com
#31378: django.test.utils compare_xml assumes string, errors on bytes
-----------------------------------+--------------------------------------

Reporter: Joe Germuska | Owner: nobody
Type: Uncategorized | Status: new
Component: Testing framework | Version: master
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
-----------------------------------+--------------------------------------
Description changed by Joe Germuska:

Old description:

> In writing a test case, I found that passing the test client's
> `response.content` object into `TestCase.assertXMLEqual()` resulted in
> this error: `a bytes-like object is required, not 'str'`
>
> In tracing the code, this originates from
> [https://github.com/django/django/blob/ba4389a36b5fb1afce0cddb4e28233138b6612b7/django/test/utils.py#L586-L587
> django.test.utils.compare_xml], where string literals `\\n` and `\n` are
> passed to `replace()` even though the types of `want` and `got`
> (particularly `got`) may be `bytes` and not `str`.
>
> The workaround is to decode the response content
> (`response.content.decode('utf-8')`) and to pass the assertion comparison
> ("want") also as a string, but if this method is going to be used in the
> testing framework, it seems that it should tolerate directly passing in
> `response.content`, even if that's a `bytes`.

New description:

In writing a test case, I found that passing the test client's
`response.content` object into `TestCase.assertXMLEqual()` resulted in
this error: `a bytes-like object is required, not 'str'`

In tracing the code, this originates from
[https://github.com/django/django/blob/ba4389a36b5fb1afce0cddb4e28233138b6612b7/django/test/utils.py#L586-L587
django.test.utils.compare_xml], where string literals `\\n` and `\n` are
passed to `replace()` even though the types of `want` and `got`
(particularly `got`) may be `bytes` and not `str`.

The workaround is to decode the response content before calling
`assertXMLEqual` (`response.content.decode('utf-8')`) and to pass the


assertion comparison ("want") also as a string, but if this method is
going to be used in the testing framework, it seems that it should
tolerate directly passing in `response.content`, even if that's a `bytes`.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/31378#comment:1>

Django

unread,
Mar 19, 2020, 1:42:40 AM3/19/20
to django-...@googlegroups.com
#31378: Support bytes in assertXMLEqual()/assertXMLNotEqual().
-----------------------------------+--------------------------------------

Reporter: Joe Germuska | Owner: nobody
Type: New feature | Status: closed

Component: Testing framework | Version: master
Severity: Normal | Resolution: wontfix

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 felixxm):

* status: new => closed
* type: Uncategorized => New feature
* resolution: => wontfix


Comment:

It's
[https://docs.djangoproject.com/en/3.0/topics/testing/tools/#django.test.SimpleTestCase.assertXMLEqual
documented] that `assertXMLEqual()` and `assertXMLNotEqual()` accept
strings. I don't think we would like to complicate these methods, you can
always use `response.content.decode()` (as you've already pointed out).

--
Ticket URL: <https://code.djangoproject.com/ticket/31378#comment:2>

Reply all
Reply to author
Forward
0 new messages