unit testing - python dynamic test generation -


i've base class yields test cases. concrete implementations of class register tests. since nose supports test generation via yield test functions (not classes) want class decorator applies necessary function wrapping.

note: reason why need yield test cases test class needs initialization cannot run in setup.

below minimal example shows expected behavior, however, works if make_generator decorator in current module - when move separate module no tests yielded. can tell me why happens?

def make_generator(cls):     """returns function instantiates ``cls`` , yields tests. """     def generator():         obj = cls()         t in obj:             yield t      generator.__name__ = 'test_%s' % cls.__name__     return generator   def register(func):     """set _check attr of ``func``. """     func._check = true     return func   class basemixin(object):      def __init__(self):         """register check handlers. """         self.check_handlers = []         attrs = (attr attr in dir(self) if not attr.startswith('__'))         attr in attrs:             func = getattr(self, attr)             if getattr(func, '_check', false):                 self.check_handlers.append(attr)      def __iter__(self):         """generate tests registered handlers. """         # stuff         args = []         check in self.check_handlers:             func = getattr(self, check)             yield func, args   @make_generator class concretetest(basemixin):      @register     def check_foobar(self, args):         assert false 

i found out why no test yielded if move make_generator decorator different module.

make_generator return function, __module__ attribute of function module in make_generator defined - not tested module , nose not run generated test function.

if set __module__ attribute of returned function works fine::

def make_generator(cls): """returns function instantiates ``cls`` , yields tests. """ def generator():     obj = cls()     t in obj:         yield t  generator.__name__ = 'test_%s' % cls.__name__ # set same module test case nose runs generator.__module__ = cls.__module__ return generator 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

c++ - End of file on pipe magic during open -