{"id":4329,"library":"xmlunittest","title":"XML Unittest","description":"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.","status":"active","version":"1.0.1","language":"en","source_language":"en","source_url":"https://github.com/Exirel/python-xmlunittest","tags":["xml","unittest","testing","lxml","xpath","schema-validation"],"install":[{"cmd":"pip install xmlunittest","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Provides core XML parsing, XPath evaluation, and schema validation capabilities.","package":"lxml","optional":false}],"imports":[{"note":"The primary class to inherit from is XmlTestCase, not the module itself.","wrong":"import xmlunittest","symbol":"XmlTestCase","correct":"from xmlunittest import XmlTestCase"}],"quickstart":{"code":"import unittest\nfrom xmlunittest import XmlTestCase\n\nclass MyXmlTests(XmlTestCase):\n    def test_basic_xml_content(self):\n        data = \"\"\"<root><item id='1'>Value A</item><item id='2'>Value B</item></root>\"\"\"\n        self.assertXmlDocument(data)\n        self.assertXpathsExist(data, ['/root', '/root/item', '/root/item[@id=\"1\"]'])\n        self.assertXpathsOnlyOne(data, ['/root/item[@id=\"1\"]'])\n        self.assertXpathValues(data, '//item[@id=\"1\"]', ['Value A'])\n        self.assertXpathAttributes(data, '//item[@id=\"1\"]', {'id': '1'})\n\n    def test_with_namespaces(self):\n        data = \"\"\"<root xmlns:ns='http://example.com/ns'><ns:element>Hello</ns:element></root>\"\"\"\n        # Pass namespaces explicitly when using XPath with namespaces\n        self.assertXpathValues(data, '//ns:element', ['Hello'], namespaces={'ns': 'http://example.com/ns'})\n\nif __name__ == '__main__':\n    unittest.main()","lang":"python","description":"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."},"warnings":[{"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.","message":"Prior 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.","severity":"breaking","affected_versions":"<0.4.0"},{"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`.","message":"The `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.","severity":"gotcha","affected_versions":"All"},{"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.","message":"Versions 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.","severity":"breaking","affected_versions":"<1.0.0"},{"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.","message":"A 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)`.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}