Skip to main content

Anyone uses XML, for RSS, for configuration files, for… well, we all use XML for our own reasons (folk says one can not simply uses XML, but still…).

So, your code generates XML, and everything is fine. As you follow best practices (if you don’t, I think you should), you have written some good unit-tests, where you compare code’s result with an expected result. I mean you compare string with string. One day, something bad might happen.

XML is not a simple string, it is a structured document. One can not simply compare two XML string and expect everything to be fine: attributes’s order can change unexpectedly, elements can be optional, and no one can explain simply how spaces and tabs works in XML formatting.

Here comes XML unittest TestCase: if you want to use the built-in unittest package (or if it is a requirement), and you are not afraid of using xpath expression with lxml, this library is made for you.

You will be able to test your XML document, and use the power of xpath and various schema languages to write tests that matter.

How to

  • Extends xmlunittest.XmlTestCase

  • Write your tests, using the function or method that generate XML document

  • Use xmlunittest.XmlTestCase‘s assertion methods to validate

  • Keep your tests readable

Example:

from xmlunittest import XmlTestCase


class CustomTestCase(XmlTestCase):

    def test_my_custom_test(self):
        # In a real case, data come from a call to your function/method.
        data = """<?xml version="1.0" encoding="UTF-8" ?>
        <root xmlns:ns="uri">
            <leaf id="1" active="on" />
            <leaf id="2" active="on" />
            <leaf id="3" active="off" />
        </root>"""

        # Everything starts with `assertXmlDocument`
        root = self.assertXmlDocument(data.encode())

        # Check namespace
        self.assertXmlNamespace(root, 'ns', 'uri')
        # Check
        self.assertXpathsUniqueValue(root, ('./leaf/@id', ))
        self.assertXpathValues(root, './leaf/@active', ('on', 'off'))

Release files for xmlunittest 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xmlunittest 1.0.1
File Size Uploaded
xmlunittest-1.0.1.tar.gz 5.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for xmlunittest 1.0.1
File Interpreter ABI Platform
xmlunittest-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.8 kB

Release files / xmlunittest-1.0.1.tar.gz

Download URL xmlunittest-1.0.1.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
1db262eac652d21a4af4548e8fb761f516918b980d9c1ba2828e4c7ff615e19f
BLAKE2b-256 checksum
How to use checksums
8f5bbf612e3cc56bdd2fd3120e45ced6f318b4019bfec174bce3cc4d3b41bd5b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / xmlunittest-1.0.1-py3-none-any.whl

Download URL xmlunittest-1.0.1-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
48d76246f3dbfd0b644d9844394e165b8f482b33996bbebe324586fe9f9cfa7a
BLAKE2b-256 checksum
How to use checksums
860ed134d4e05138ed98ddbf427d1fc5a54d8ff25fd5418a1bf27af8df24d460
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

0.5.0

1 release file

0.4.0

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.1

1 release file

0.1.0

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page