Pyunit test suite builder

Pyunit is a unit tests framework for Python. I believe it is the standard one. It works very well, however to run all the tests within a folder might be not trivial. You can do either hardcode the tests names to build the test suite or use this helper. The helper uses introspection (what is called reflection on .Net):

  2. import unittest
  3. import os
  4. import re
  5. import sys
  6. import inspect
  7. import types
  9. def makeSuite(testsFolder = './tests', testsPackageName = 'tests', testClassSuffix='Tests'):
  10. """Given the testsFolder parameter, loads all the .py files to search for classes
  11. which name contains testClassSuffix and inherit from unittest.TestCase.
  12. It takes these classes to buid the TestSuite and run all the tests.
  13. """
  14. testClasses = []
  15. testModules = {}
  16. testPackage = None
  17. suite = unittest.TestSuite()
  18. folder = os.listdir(testsFolder)
  19. for moduleName in folder:
  20. modulePath = os.path.join(testsFolder, moduleName)
  21. if not os.path.isdir(modulePath) and".py$", moduleName) :
  22. fullModuleName = testsPackageName + '.' + moduleName[:-3]
  23. # curious: this imports the package not the module
  24. testPackage = __import__(fullModuleName)
  25. # it is weird indeed as __import__('packageName') does not load its modules
  26. for packageItem in dir(testPackage):
  27. if, packageItem):
  28. if not testModules.has_key(packageItem):
  29. testModules[packageItem] = True
  30. moduleHandler = getattr(testPackage, packageItem)
  31. for className, classHandler in inspect.getmembers(moduleHandler, callable):
  32. if inspect.isclass(classHandler) and issubclass(classHandler, unittest.TestCase):
  33. testClasses.append(classHandler)
  34. for classHandler in testClasses:
  35. for name, value in inspect.getmembers(classHandler, callable):
  36. if re.match("test", name):
  37. print " - Test:", name
  38. suite.addTest(classHandler(name))
  39. return suite
  41. if __name__ == "__main__":
  42. suite = makeSuite()
  43. runner = unittest.TextTestRunner()
  • esauro

    Django extends unittest framework adding some web application specific features.
    It uses conventions to create automatically the test suite (the tests must be located inside or and be subclasses of unittest.TestCase). As well as this you can use doctests too.

  • Carlos Ble

    Thanks mate,
    I’m using Django for the ORM only so far. Most parts of SIGA project don’t have to do with web interfaces.
    Are there more features in the django unit testing frammework that are not in Pyunit? (other than the web acceptance testing)

  • esauro

    I think you may found interesting how Django manage databases in tests suites. I use empty database filled with fixtures data therefore you can be sure of certain data are in the database. You can check the documentation for more.

  • Tory

    Fantastic! This is exactly what I needed. Before committing, my team is much more likely to run one test script than twenty. Thank you so, so much.

  • Carlos Ble

    Glad you find it useful! 🙂

  • cheap ugg boots

    Your RSS feed doesn’t work in my browser (google chrome) how could I correct it?