2009-12-01 13:36:59 -06:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
import os, os.path, sys
|
2012-06-02 07:36:49 -05:00
|
|
|
try:
|
|
|
|
from AccessControl.SecurityManagement import \
|
|
|
|
newSecurityManager, noSecurityManager
|
|
|
|
except ImportError:
|
|
|
|
pass
|
2009-12-01 13:36:59 -06:00
|
|
|
|
2009-11-11 13:22:13 -06:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class TestMixin:
|
2011-12-05 08:11:29 -06:00
|
|
|
'''This class is mixed in with any ZopeTestCase.'''
|
2009-12-01 13:36:59 -06:00
|
|
|
def getNonEmptySubModules(self, moduleName):
|
2011-02-17 11:13:42 -06:00
|
|
|
'''Returns the list of sub-modules of p_app that are non-empty.'''
|
2009-12-01 13:36:59 -06:00
|
|
|
res = []
|
|
|
|
try:
|
|
|
|
exec 'import %s' % moduleName
|
|
|
|
exec 'moduleObj = %s' % moduleName
|
|
|
|
moduleFile = moduleObj.__file__
|
|
|
|
if moduleFile.endswith('.pyc'):
|
|
|
|
moduleFile = moduleFile[:-1]
|
|
|
|
except ImportError, ie:
|
|
|
|
return res
|
|
|
|
except SyntaxError, se:
|
|
|
|
return res
|
|
|
|
# Include the module if not empty. "Emptyness" is determined by the
|
|
|
|
# absence of names beginning with other chars than "__".
|
|
|
|
for elem in moduleObj.__dict__.iterkeys():
|
|
|
|
if not elem.startswith('__'):
|
|
|
|
res.append(moduleObj)
|
|
|
|
break
|
|
|
|
# Include sub-modules if any
|
|
|
|
if moduleFile.find("__init__.py") != -1:
|
|
|
|
# Potentially, sub-modules exist.
|
|
|
|
moduleFolder = os.path.dirname(moduleFile)
|
|
|
|
for elem in os.listdir(moduleFolder):
|
|
|
|
if elem.startswith('.'): continue
|
|
|
|
subModuleName, ext = os.path.splitext(elem)
|
|
|
|
if ((ext == '.py') and (subModuleName != '__init__')) or \
|
|
|
|
os.path.isdir(os.path.join(moduleFolder, subModuleName)):
|
|
|
|
# Submodules may be sub-folders or Python files
|
|
|
|
subModuleName = '%s.%s' % (moduleName, subModuleName)
|
|
|
|
res += self.getNonEmptySubModules(subModuleName)
|
|
|
|
return res
|
|
|
|
|
2009-12-03 09:45:05 -06:00
|
|
|
@staticmethod
|
|
|
|
def getCovFolder():
|
2009-12-01 13:36:59 -06:00
|
|
|
'''Returns the folder where to put the coverage folder if needed.'''
|
|
|
|
for arg in sys.argv:
|
|
|
|
if arg.startswith('[coverage'):
|
|
|
|
return arg[10:].strip(']')
|
2014-05-14 08:10:41 -05:00
|
|
|
return
|
2012-06-02 07:36:49 -05:00
|
|
|
def _setup(self): pass
|
|
|
|
|
2009-11-11 13:22:13 -06:00
|
|
|
# Functions executed before and after every test -------------------------------
|
|
|
|
def beforeTest(test):
|
2009-12-01 13:36:59 -06:00
|
|
|
'''Is executed before every test.'''
|
2009-11-11 13:22:13 -06:00
|
|
|
g = test.globs
|
2011-12-05 08:11:29 -06:00
|
|
|
g['tool'] = test.app.config.appy()
|
2009-12-01 13:36:59 -06:00
|
|
|
cfg = g['tool'].o.getProductConfig()
|
|
|
|
g['appFolder'] = cfg.diskFolder
|
2009-11-11 13:22:13 -06:00
|
|
|
moduleOrClassName = g['test'].name # Not used yet.
|
|
|
|
# Initialize the test
|
|
|
|
g['t'] = g['test']
|
|
|
|
|
2009-12-01 13:36:59 -06:00
|
|
|
def afterTest(test):
|
|
|
|
'''Is executed after every test.'''
|
|
|
|
g = test.globs
|
2009-12-03 09:45:05 -06:00
|
|
|
appName = g['tool'].o.getAppName()
|
|
|
|
exec 'from Products.%s import cov, covFolder, totalNumberOfTests, ' \
|
|
|
|
'countTest' % appName
|
|
|
|
countTest()
|
|
|
|
exec 'from Products.%s import numberOfExecutedTests' % appName
|
|
|
|
if cov and (numberOfExecutedTests == totalNumberOfTests):
|
2009-12-01 13:36:59 -06:00
|
|
|
cov.stop()
|
2009-12-03 09:45:05 -06:00
|
|
|
appModules = test.getNonEmptySubModules(appName)
|
2009-12-14 13:22:55 -06:00
|
|
|
# Dumps the coverage report
|
|
|
|
# HTML version
|
2009-12-03 09:45:05 -06:00
|
|
|
cov.html_report(directory=covFolder, morfs=appModules)
|
2009-12-14 13:22:55 -06:00
|
|
|
# Summary in a text file
|
|
|
|
f = file('%s/summary.txt' % covFolder, 'w')
|
|
|
|
cov.report(file=f, morfs=appModules)
|
|
|
|
f.close()
|
|
|
|
# Annotated modules
|
|
|
|
cov.annotate(directory=covFolder, morfs=appModules)
|
2009-11-11 13:22:13 -06:00
|
|
|
# ------------------------------------------------------------------------------
|