[Python-Dev] test_expat.py and unittest

Jerry Seutter jseutter at gmail.com
Wed Mar 21 06:37:12 CET 2007


Hi,

I have been working on converting some of the older test files to use
unittest.  test_expat.py has a section like:

class Outputter:
    def StartElementHandler(self, name, attrs):
        print 'Start element:\n\t', repr(name), sortdict(attrs)

    def EndElementHandler(self, name):
        print 'End element:\n\t', repr(name)

    def CharacterDataHandler(self, data):
        data = data.strip()
        if data:
            print 'Character data:'
            print '\t', repr(data)

    def ProcessingInstructionHandler(self, target, data):
        print 'PI:\n\t', repr(target), repr(data)

    def StartNamespaceDeclHandler(self, prefix, uri):
        print 'NS decl:\n\t', repr(prefix), repr(uri)
    <snip>

...where it defines a set of handlers for an xml document that prints the
output to stdout.  The test then parses an xml document using this class and
the stdout output is compared to a file.  There are several lines of text on
stdout to be compared:

PI:
        'xml-stylesheet' 'href="stylesheet.css"'
Comment:
        ' comment data '
Notation declared: ('notation', None, 'notation.jpeg', None)
Unparsed entity decl:
        ('unparsed_entity', None, 'entity.file', None, 'notation')
Start element:
        'root' {'attr1': 'value1', 'attr2': 'value2\xe1\xbd\x80'}
NS decl:
        'myns' 'http://www.python.org/namespace'
Start element:
        'http://www.python.org/namespace!subelement' {}
Character data:
        'Contents of subelements'
End element:
        'http://www.python.org/namespace!subelement'
End of NS decl:
        'myns'
Start element:
        'sub2' {}
Start of CDATA section
Character data:
        'contents of CDATA section'
End of CDATA section
End element:
        'sub2'
External entity ref: (None, 'entity.file', None)
End element:
        'root'


unittest does not seem well suited to this type of testing, because the
stdout output comparison is testing many different pieces of functionality.
Some subset of it is probably even important.  To do this same testing with
unittest would require many lines of self.assertEquals(expected_string,
string_from_stdout).

Does anyone have ideas on how this can be tested in a manner that is not as
brittle as the stdout tests, but doesn't require writing significantly more
test code?  Or if not, is converting this file to use unittest a bad idea?

Jerry Seutter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-dev/attachments/20070320/26f04867/attachment.html 


More information about the Python-Dev mailing list