Skip to main content

Testing XML with unittest

This library is a set of helper methods for testing XML with Python’s unittest library. The new assertion methods use XPath to select the XML elements that assertions are being made about.

Sample usage

To use these additional assertions, simply mix the xmltest.XMLAssertions class into your test class:

import unittest
from xmltest import XMLAssertions

class MyTestCase(unittest.TestCase, XMLAssertions):
    pass

Now suppose you have the following XML string that you wish to examine:

response_xml = """<?xml version="1.0" encoding="UTF-8" ?>
<Response>
    <CardTxn>
        <authcode>060642</authcode>
        <card_scheme>Switch</card_scheme>
        <issuer country="UK">HSBC</issuer>
    </CardTxn>
    <reference>3000000088888888</reference>
    <merchantreference>1000001</merchantreference>
    <mode>LIVE</mode>
    <reason>ACCEPTED</reason>
    <status>1</status>
    <time>1071567305</time>
</Response>"""

Assert the number of elements matching an XPath query:

self.assertXPathNodeCount(response_xml, 1, 'CardTxn/issuer[@country="UK"]')
self.assertXPathNodeCount(response_xml, 1, 'status')

Assert the value of a particular element:

self.assertXPathNodeText(response_xml, 'ACCEPTED', 'reason')

Assert the attribute values of an element:

self.assertXPathNodeAttributes(xml, {'country': 'UK'}, 'CardTxn/issuer'}

Installation

From PyPi:

pip install unittest-xml

Contributing

To run the tests, install nose and mock:

pip install nose mock

and use:

nosetests

Metadata

Release files for unittest-xml 0.2.2

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

Source distribution (sdist)

Source distribution for unittest-xml 0.2.2
File Size Uploaded Provenance Actions
unittest-xml-0.2.2.tar.gz 2.7 kB Without provenance Details for unittest-xml-0.2.2.tar.gz

Release files / unittest-xml-0.2.2.tar.gz

Download URL unittest-xml-0.2.2.tar.gz
Size 2.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0da7019577f2c2ead72de54d29166fcdb00a130e3efa3afffcd398a656a67596
BLAKE2b-256 checksum
How to use checksums
25de0a7e5151f444e051655a393da080f44acef403b94a5d2252d59d5c780cf0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for unittest-xml 0.2.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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