[Python-Dev] test_expat.py and unittest
Guido van Rossum
guido at python.org
Wed Mar 21 06:43:34 CET 2007
You could use doctest, which is an accepted testing tool that lets you
compare snippets of output directly without having to check in a
"golden" file. Despite what is written somewhere, it is not deprecated
or discouraged. See Lib/doctest.py.
You could also use some advanced testing strategy where the callback,
instead of printing, append something to a list (perhaps an instance
variable), and later you check that the list contains the expected
sequence of values.
--Guido
On 3/20/07, Jerry Seutter <jseutter at gmail.com> wrote:
> 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
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/guido%40python.org
>
>
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev
mailing list