Additional assertion methods for testing XML with python's unittest.
Latest commit 45cccef Jan 13, 2012 @codeinthehole Bumped version
Failed to load latest commit information.
tests Reworked all methods to use XPath Jan 13, 2012
.gitignore Updated type of exception raised Jan 12, 2012 Fixed issue with README missing from package Jan 13, 2012
README.rst Reworked all methods to use XPath Jan 13, 2012 Bumped version Jan 13, 2012


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):

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

response_xml = """<?xml version="1.0" encoding="UTF-8" ?>
        <issuer country="UK">HSBC</issuer>

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'}


From PyPi:

pip install unittest-xml


To run the tests, install nose and mock:

pip install nose mock

and use: