Registry / testing / xmlunittest

xmlunittest

JSON →
library1.0.1pypypi✓ verified 24d ago

xmlunittest is a Python library that extends the built-in `unittest` framework to provide robust assertion methods for testing XML documents. Leveraging `lxml` and XPath, it allows users to validate XML structure, content, and schema conformance, avoiding the pitfalls of direct XML string comparisons. The current version is 1.0.1, with releases typically focusing on compatibility updates and feature enhancements for XML processing.

pip install xmlunittest
INSTALL
IMPORT
SIG · XMLUNITTEST
X
xmlunittest
testingpythonv1.0.1
Install
2.8s avg
Import
279ms
Disk
28MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.288s · 29.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.8s · import 0.270s · 30MB
28MB installed
● package 28MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

XmlTestCase
from xmlunittest import XmlTestCase
import xmlunittest
The primary class to inherit from is XmlTestCase, not the module itself.

This quickstart demonstrates how to create an XML test case by inheriting from `xmlunittest.XmlTestCase`. It shows common assertions like `assertXmlDocument` for basic validity, `assertXpathsExist` to check for elements, `assertXpathValues` to check content, and `assertXpathAttributes` to check attributes. It also includes an example of handling XML with namespaces by passing a `namespaces` dictionary to the assertion methods.

import unittest from xmlunittest import XmlTestCase class MyXmlTests(XmlTestCase): def test_basic_xml_content(self): data = """<root><item id='1'>Value A</item><item id='2'>Value B</item></root>""" self.assertXmlDocument(data) self.assertXpathsExist(data, ['/root', '/root/item', '/root/item[@id="1"]']) self.assertXpathsOnlyOne(data, ['/root/item[@id="1"]']) self.assertXpathValues(data, '//item[@id="1"]', ['Value A']) self.assertXpathAttributes(data, '//item[@id="1"]', {'id': '1'}) def test_with_namespaces(self): data = """<root xmlns:ns='http://example.com/ns'><ns:element>Hello</ns:element></root>""" # Pass namespaces explicitly when using XPath with namespaces self.assertXpathValues(data, '//ns:element', ['Hello'], namespaces={'ns': 'http://example.com/ns'}) if __name__ == '__main__': unittest.main()
Debug
Known issues
breakingPrior to version 0.4.0, `xmlunittest` did not explicitly handle namespaces in XPath expressions. If your tests relied on XPath against namespaced XML, you might need to adjust your XPath expressions to properly declare and use namespaces (e.g., `{ns:uri}localname` or by passing a `namespaces` dictionary to assertion methods) after upgrading, or ensure your XML matches a non-namespaced structure if that was the prior implicit assumption.
fix
Ensure XPath expressions correctly reference namespaces (e.g., by providing a `namespaces` dictionary to assertion methods) or adjust XML if non-namespaced XPath was implicitly relied upon.
affects: <0.4.0
gotchaThe `xmlunittest` library depends on `lxml`, which can sometimes be challenging to install due to underlying system library requirements (e.g., `libxml2`, `libxslt`). Users might encounter compilation errors if these development headers are not present on their system.
fix
Before installing `lxml` (and thus `xmlunittest`), ensure that necessary system libraries and development headers for `libxml2` and `libxslt` are installed. For example, on Debian/Ubuntu: `sudo apt-get install libxml2-dev libxslt1-dev python3-dev`.
affects: All
breakingVersions of `xmlunittest` prior to 1.0.0 (specifically up to 0.3.2) supported Python 2.7. The current version (1.0.1) requires Python >= 3.8. Migrating from older versions on Python 2.x to the latest will require a Python 3 environment.
fix
Upgrade your Python environment to 3.8 or newer. For projects requiring Python 2.7, stick to `xmlunittest` versions <= 0.3.2, but be aware these are unmaintained.
affects: <1.0.0
gotchaA common mistake when testing XML is to perform direct string comparisons. `xmlunittest` explicitly aims to solve the issues arising from such comparisons (e.g., attribute order, whitespace, optional elements). Users should leverage `XmlTestCase`'s specialized assertion methods for robust XML validation, rather than `assertEqual(xml_string_1, xml_string_2)`.
fix
Always use the assertion methods provided by `xmlunittest.XmlTestCase` (e.g., `assertXmlDocument`, `assertXpathValues`, `assertXpathsExist`) instead of standard `unittest.TestCase` assertions for comparing XML strings.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'xmlunittest'
The `xmlunittest` library is not installed in the current Python environment.
fix
pip install xmlunittest
AttributeError: 'MyTestClass' object has no attribute 'assertXmlEquivalent'
The test class does not inherit from `xmlunittest.XmlTestCase`, which provides the XML assertion methods.
fix
from xmlunittest import XmlTestCase

class MyTestClass(XmlTestCase):
    # ... tests using assertXmlEquivalent, etc.
lxml.etree.XPathSyntaxError: Invalid expression
An XPath expression provided to an `xmlunittest` method is syntactically incorrect.
fix
Correct the XPath expression to be valid according to XPath syntax (e.g., ensure proper prefixes, axes, and predicate formatting).
Example: `self.assertXmlHasXPath(xml_doc, "/root/item[@id='1']")`
TypeError: XML content must be a string, a file-like object, or an lxml.etree.Element
An `xmlunittest` assertion method received an argument for XML content that is not a string, a file-like object, or an `lxml.etree.Element`.
fix
Ensure that the XML content passed to `xmlunittest` methods is a valid string, a file-like object (e.g., `io.StringIO`), or an `lxml.etree.Element`.
Upgrade
Version history
1.0.1latest on PyPI · released Jul 13, 2024
Audit
Dependencies
lxmlrequiredProvides core XML parsing, XPath evaluation, and schema validation capabilities.
Agent activity
19 hits · last 30 days
node
16
Resources
xmlunittest — pip install xmlunittest · libregistry