Chapter 10. XMLUnit
Overview
XMLUnit provides useful support for unit testing of XML content. It is an extension to JUnit and NUnit, rather than a standalone framework.
Development and testing of software that generates XML content are common activities. Writing such tests is difficult with generic unit test frameworks. XML tests often compare expected XML content to actual XML content generated by the software that is being tested. Such tests can be written by doing a string comparison between the expected and actual XML content strings. However, what if the actual XML content contains a return character for readability, or puts XML attributes in opposite order than they appear in the expected XML string? Taken as XML documents, the two XML strings have the same contents, but an exact string comparison will fail.
To further illustrate the problem, the following pieces of XML
represent an element named book containing the
child elements title and
author. Since the child elements are not in the
same order, these XML elements have equivalent content, but different
syntax:
<book> <title>Dune</title> <author>Frank Herbert</author> </book> <book> <author>Frank Herbert</author> <title>Dune</title> </book>
The following representations of an empty XML element named
library also have equal content but different
syntax:
<library> </library> <library/>
It is clearly useful to be able to test that two pieces of XML have the same content despite differences in format.
Other common test cases include ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access