10
Jun
6

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):

 
import unittest
import os
import re
import sys
import inspect
import types
 
def makeSuite(testsFolder = './tests', testsPackageName = 'tests', testClassSuffix='Tests'):
    """Given the testsFolder parameter, loads all the .py files to search for classes
       which name contains testClassSuffix and inherit from unittest.TestCase.
       It takes these classes to buid the TestSuite and run all the tests.
    """
    testClasses = []
    testModules = {}
    testPackage = None
    suite = unittest.TestSuite()
    folder = os.listdir(testsFolder)
    for moduleName in folder:
        modulePath = os.path.join(testsFolder, moduleName)
        if not os.path.isdir(modulePath) and re.search(".py$", moduleName) :
            fullModuleName = testsPackageName + '.' + moduleName[:-3]
            # curious: this imports the package not the module
            testPackage = __import__(fullModuleName)
            # it is weird indeed as __import__('packageName') does not load its modules
            for packageItem in dir(testPackage):
                if re.search(testClassSuffix, packageItem):
                    if not testModules.has_key(packageItem):
                        testModules[packageItem] = True
                        moduleHandler = getattr(testPackage, packageItem)
                        for className, classHandler in inspect.getmembers(moduleHandler, callable):
                            if inspect.isclass(classHandler) and issubclass(classHandler, unittest.TestCase):
                                testClasses.append(classHandler)
    for classHandler  in testClasses:
        for name, value in inspect.getmembers(classHandler, callable):
            if re.match("test", name):
                print " - Test:", name
                suite.addTest(classHandler(name))
    return suite
 
if __name__ == "__main__":
    suite = makeSuite()
    runner = unittest.TextTestRunner()
    runner.run(suite)
 
Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.
  • http://jadarios.wordpress.com 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 models.py or tests.py and be subclasses of unittest.TestCase). As well as this you can use doctests too.

  • http://carlosble.com 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)

  • http://jadarios.wordpress.com 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. http://www.djangoproject.com/documentation/testing/

  • http://www.thetoryparty.com 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.

  • http://carlosble.com Carlos Ble

    Glad you find it useful! :-)

  • http://www.bestuggcheapsale.com/ cheap ugg boots

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

Celadon theme by the Themes Boutique
teeth bleaching
baldness treatment