Failing unittest Test cases

Scott David Daniels scott.daniels at acm.org
Thu Jan 12 10:12:04 EST 2006


Duncan Booth wrote:
> ... Possible enhancements:

> add another argument for associated issue tracker id ... some unbroken tests 
 > will also have associated issues this might just be a separate decorator.

This is probably easier to do as a separate decoration which would have to
precede the "failing test" decoration:
     def tracker(identifier):
         def markup(function):
             function.tracker = identifier
             return function
         return markup

> add some easyish way to generate a report of broken tests.

Here's a generator for all the "marked broken" tests in a module:

     import types, unittest

     def marked_broken(module):
         for class_name in dir(module):
             class_ = getattr(module, class_name)
             if (isinstance(class_, (type, types.ClassType)) and
		        issubclass(class_, unittest.TestCase)):
                 for test_name in dir(class_):
                     if test_name.startswith('test'):
                         test = getattr(class_, test_name)
                         if (hasattr(test, '__name__') and
                                  test.__name__.startswith('XXX_')):
                             yield class_name, test_name, test.todo


You could even use it like this:

     import sys
     import mytests

     for module_name, module in sys.modules.iteritems():
         last_class = ''
         for class_name, test_name, reason in marked_broken(module):
             if module_name:
                 print 'In module %s:' % module_name
                 module_name = ''
             if last_class != class_name:
                 print 'class', class_name
                 last_class = class_name
             print '  %s\t %s' % (test_name, reason)


Thanks for the thoughtful feedback.

--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list