Getting PyUnit to run Package Test Modules

Casey McGinty casey.mcginty at gmail.com
Tue Apr 22 06:36:57 EDT 2008


I came up with this solution based off of the __import__ python reference
page. If I missed anything let me know.

def suite():
   # create TestSuite object
   alltests = unittest.TestSuite()
   # load all modules define in the module list
   for name in mod_to_test:
      print name
      mod = __import__(name)
      components = name.split('.')
      for comp in components[1:]:
         print comp
         mod = getattr(mod,comp)
      alltests.addTest(unittest.findTestCases(mod))
   return alltest

On Tue, Apr 22, 2008 at 12:12 AM, Casey McGinty <casey.mcginty at gmail.com>
wrote:

> Hopefully this is an easy question for someone to answer. I have a
> directory structure like so:
>
> alltest.py
> prog.py
> ../package
>      __init__.py
>      mod1.py
>      test_mod1.py
>      modn. py
>      (and so on...)
>
> Each test_mod*.py file contains some PyUnit test cases. I am using the
> following code in alltest.py to run all the unit test modules:
>
> mod_to_test = [package.mod1, package.mod2]
>
> def suite():
>    # create TestSuite object
>    alltests = unittest.TestSuite()
>    # load all modules define in the module list
>    for module in map(__import__, mod_to_test):
>       alltests.addTest(unittest.findTestCases(module))
>    return alltest
>
> if __name__ == '__main__':
>    unittest.main(defaultTest='suite')
>
> My guess is there is something needed in __init__.py to get this work. Any
> advice?
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080422/840c8d36/attachment-0001.html>


More information about the Python-list mailing list