Opened 5 years ago

Closed 5 years ago

#30497 closed Bug (fixed)

assertXMLEqual fails on document type declaration.

Reported by: Yuri Kanivetsky Owned by: Caio Ariede
Component: Testing framework Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Yuri Kanivetsky)

Prepare Django project:

$ python -m venv env
$ . ./env/bin/activate
$ pip install django
$ django-admin startproject p1
$ cd p1

Create p1/tests.py:

from django.test import TestCase

class MyTestCase(TestCase):
    def test_assert_xml_equal(self):
        xml1 = '''
            <?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE root SYSTEM "example.dtd">
            <root />
        '''
        xml2 = '''
            <?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE root SYSTEM "example.dtd">
            <root />
        '''
        self.assertXMLEqual(xml1, xml2)

Run the tests:

$ ./manage.py test
Creating test database for alias 'default'...
F
======================================================================
FAIL: test_assert_xml_equal (p1.tests.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/yuri/_/1/env/lib/python3.7/site-packages/django/test/testcases.py", line 843, in assertXMLEqual
    result = compare_xml(xml1, xml2)
  File "/home/yuri/_/1/env/lib/python3.7/site-packages/django/test/utils.py", line 598, in compare_xml
    return check_element(want_root, got_root)
AttributeError: 'DocumentType' object has no attribute 'tagName'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/yuri/_/1/p1/p1/tests.py", line 15, in test_assert_xml_equal
    self.assertXMLEqual(xml1, xml2)
  File "/home/yuri/_/1/env/lib/python3.7/site-packages/django/test/testcases.py", line 846, in assertXMLEqual
    self.fail(self._formatMessage(msg, standardMsg))
AssertionError: First or second argument is not valid XML
'DocumentType' object has no attribute 'tagName'

----------------------------------------------------------------------
Ran 1 test in 0.002s

FAILED (failures=1)
Destroying test database for alias 'default'...
System check identified no issues (0 silenced).

That happens because django.utils expects first non-comment element to be the root element. Which is generally not the case.

Change History (6)

comment:1 by Yuri Kanivetsky, 5 years ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 5 years ago

Summary: assertXMLEqual chokes on document type declarationassertXMLEqual fails on document type declaration.
Triage Stage: UnreviewedAccepted
Version: 2.2master

Thanks for this report. We should probably omit Node.DOCUMENT_TYPE_NODE when looking for a root element and compare document types separately.

comment:3 by Caio Ariede, 5 years ago

Owner: changed from nobody to Caio Ariede
Status: newassigned

comment:5 by Mariusz Felisiak, 5 years ago

Has patch: set

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In 753b67c:

Fixed #30497 -- Ignored document type in assertXMLEqual()/assertXMLNotEqual().

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