Skip to main content

Additional assertion methods for testing XML

Project description

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

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

unittest-xml-0.2.1.tar.gz (2.7 kB view details)

Uploaded Source

File details

Details for the file unittest-xml-0.2.1.tar.gz.

File metadata

File hashes

Hashes for unittest-xml-0.2.1.tar.gz
Algorithm Hash digest
SHA256 7bce10a83b34ca2befa5800dfd6c8637ab58650bb15d8e1e230169c21e675182
MD5 bf9b3eef335ecbda101ae85997a7bd59
BLAKE2b-256 3fc416e609a8399588a7c5c162f2520debe50c0b4e347aae3879513341935f2f

See more details on using hashes here.

Supported by

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