[Pyunit-interest] Some PyUnit snippets
Brought to you by:
purcell
From: Joshua Macy/T. P. <Ma...@to...> - 2001-02-13 12:16:23
|
Here are a couple of things that I do all the time, just in case anyone is interested. (I think both might be relevant to the current discussion on the Python Dev in regard to using PyUnit as Python's official unit test module.) Syntactic sugar for common tests: def assertEquals(got, expect, error): assert got == expect, '''%s: expected %s, got %s''' % (error, `expect`, `got`) Invocation of Tim Peters' doctext module (also becoming part of Python core): class DocTestCase(unittest.TestCase): def setUp(self): """Override setUp in derived classes to set the doctest to the appropriate doctest object from the module. Usually this looks like: import doctest, mymodule self.doctest = doctest.testmod(mymodule, verbose=0) """ self.doctest = None def checkDoc(self): if self.doctest: errors, tests = self.doctest(0) assertEquals(errors, 0, '%s Doctest failed, %s errors out of %s tests' % (self.__class__.__name__, errors, tests)) It might actually make more sense to override DocTestCase's __init__ to set self.doctest, since there's no real reason to create a doctest object every time. On the other hand, a DocTestCase will almost never have more than one check method, so performance-wise it's probably moot. Joshua |