Skip to main content

Human-readable XML comparison for testing with clean diff output

Project description

xmlassert

xmlassert is a Python library designed for comparing XML documents in tests, providing clean, human-readable diffs when assertions fail. Unlike raw XML comparison tools, it ignores formatting differences and focuses on semantic equivalence.

Features

  • Human-readable diffs: Get clean, formatted diff output instead of cryptic XML comparisons
  • Formatting-agnostic: Ignores whitespace, indentation, and formatting variations
  • Secure parsing: Uses secure XML parsing practices by default
  • Test-friendly: Perfect for pytest, unittest, and other testing frameworks

Installation

pip install xmlassert

Quick Start

from xmlassert import assert_xml_equal

# These will pass (formatting differences are ignored)
assert_xml_equal("<root><a>text</a></root>", "<root>\n  <a>text</a>\n</root>")

# This will fail with a clean diff
try:
    assert_xml_equal(
        "<root><a>expected</a></root>",
        "<root><a>actual</a></root>"
    )
except AssertionError as e:
    print(e)  # Shows beautiful diff output

Usage Examples

Basic XML Comparison

from xmlassert import assert_xml_equal

# Passes - formatting differences are ignored
xml1 = "<root><element>value</element></root>"
xml2 = """
<root>
    <element>value</element>
</root>
"""
assert_xml_equal(xml1, xml2)

Pytest Example

import pytest
from xmlassert import assert_xml_equal

def test_xml_generation():
    generated_xml = generate_xml()  # Your function
    expected_xml = """
    <config>
        <setting enabled="true">value</setting>
    </config>
    """
    assert_xml_equal(generated_xml, expected_xml)

Handling XML with Comments

from xmlassert import assert_xml_equal

# Comments are ignored by default in comparison
xml_with_comments = """
<root>
    <!-- This comment is ignored -->
    <element>value</element>
</root>
"""
xml_without_comments = "<root><element>value</element></root>"
assert_xml_equal(xml_with_comments, xml_without_comments)  # Passes

Sample Output

When comparison fails, you get clean, readable diffs:

AssertionError: XML documents differ:
--- expected
+++ actual
@@ -1,3 +1,3 @@
 <root>
-  <element>expected value</element>
+  <element>actual value</element>
 </root>

Instead of the unreadable:

AssertionError: XML mismatch: <root><element>expected value</element></root> != <root><element>actual value</element></root>

Troubleshooting

Namespace Handling

XML namespaces are compared strictly. Ensure your XML uses consistent namespace declarations.

Large XML Documents

For very large documents, consider using dedicated XML diff tools. xmlassert is optimized for test-sized XML snippets.

Encoding Issues

Ensure both XML strings use the same encoding (UTF-8 recommended).

Why xmlassert?

Compared to other XML testing approaches:

Feature xmlassert xml.etree xmlunit string compare
Human-readable diffs Yes No Partial No
Formatting-agnostic Yes No Yes No
Easy to use Yes Partial Partial Yes
Secure parsing Yes Partial Yes No

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xmlassert-0.1.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xmlassert-0.1.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file xmlassert-0.1.0.tar.gz.

File metadata

  • Download URL: xmlassert-0.1.0.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.18

File hashes

Hashes for xmlassert-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8bdbf2a566847d409b2e987bf08d076b92edc1a2e843a255fa1f79eba7bad699
MD5 4c0c26848d92578772df7be06f8d8097
BLAKE2b-256 2511de9c2648ecca41a78f08e3f4ce688d213790a8e003b07499015f522176c4

See more details on using hashes here.

File details

Details for the file xmlassert-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: xmlassert-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.18

File hashes

Hashes for xmlassert-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3f645a675a7b2093afaf50bee1e313352466b4c992d3e3a6464d8bede777f7d7
MD5 9a183e1cfa47a0a14053a49a3971739f
BLAKE2b-256 65703a9704b353404dfc0c22030e1ea49de4823122a7f6f4e4e945e9baaf9e70

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page