Quick tour

how to create your first PyXMLUnit test

First thing you need to do is unpack the PyXMLUnit distributable file on your Python sypath.
Make sure you have the latest release of PyXML installed.

Now you can start writing your first test, and for this we are going to need to import the XMLTestCase module of PyXMLUnit
(which is indeed a wrapper of the PyUnit TestCase module)

from xmltestcase import XMLTestCase

Once the import is done, the next step is to create a test class:


class TestMessageTransformer(XMLTestCase):
       
    def testMessageIsTransformed(self):
        transformer = MessageTransformer("<message>Say hello...</message>")
        self.assertXMLEqual("<message>Hello World!</message>", transformer.transform())


...and after we have implemented our test class then we do implement our code.
Feels like we're doing something wrong... testing first and then developing??  Well, this is part of the test driven development (TDD) process, but let's keep focused on the example.

A very simple implementation of our MessageTransformer could be this one:

class MessageTransformer:
    def __init__(self, msg) :
        self.message = msg
       
    def transform(self) :
        # let's imagine that an XML manipulation happens here
        # and the result is: "<message>Hello World!</message>"
        return "<message>Hello World!</message>"


Right, we are now  ready to run the unit test for the MessageTransformer clas:
.
$python test_messagetransformer.py
.
----------------------------------------------------------------------
Ran 1 test in 0.010s

OK


Excellent, we managed to write our first XML unit test with practically no time!

Now, you can try to change your MessageTransformer to provide him with a different implementation of the transform method (remember that now you can run the test after every change you do).

Download this example: test_messagetransformer.py