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.
Subscribe to the RSS feed and have all new posts delivered straight to you.
-
http://jadarios.wordpress.com esauro
-
http://carlosble.com Carlos Ble
-
http://jadarios.wordpress.com esauro
-
http://www.thetoryparty.com Tory
-
http://www.bestuggcheapsale.com/ cheap ugg boots

