Commit 88980cbb authored by Grégory Wisniewski's avatar Grégory Wisniewski

Some changes in the tests runner, handle import errors to avoid a global crach

Add comments to improve this tool later


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@868 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent a1f55c5d
...@@ -20,15 +20,24 @@ import unittest ...@@ -20,15 +20,24 @@ import unittest
import logging import logging
import time import time
# TODO:
# - include failed imports in the report
# - group tests by kind (unit, functionnal...)
# - import each test case instead of modules to reduce import errors
# list of test modules # list of test modules
# each of them have to import its TestCase classes # each of them have to import its TestCase classes
TEST_MODULES = [ UNIT_TEST_MODULES = [
'neo.tests', 'neo.tests',
'neo.client.tests', 'neo.client.tests',
'neo.master.tests', 'neo.master.tests',
'neo.storage.tests' 'neo.storage.tests'
] ]
FUNC_TEST_MODULES = [
'neo.client.tests.testZODB',
]
# configuration # configuration
WITH_ZODB_TESTS = False WITH_ZODB_TESTS = False
SEND_REPORT = False SEND_REPORT = False
...@@ -53,18 +62,33 @@ if CONSOLE_LOG: ...@@ -53,18 +62,33 @@ if CONSOLE_LOG:
handler.setFormatter(formatter) handler.setFormatter(formatter)
logger.addHandler(handler) logger.addHandler(handler)
class NeoTestResult(unittest.TestResult): class NeoTestRunner(unittest.TestResult):
""" Custom result class to build report with statistics per module """ """ Custom result class to build report with statistics per module """
def __init__(self):
unittest.TestResult.__init__(self)
self.modulesStats = {}
self.lastStart = None
def run(self, name, modules, prefix='test'):
suite = unittest.TestSuite()
loader = unittest.defaultTestLoader
loader.testMethodPrefix = prefix
for test_module in modules:
try:
test_module = __import__(test_module, globals(), locals(), ['*'])
except ImportError, err:
print "Import of %s failed : %s" % (test_module, err)
continue
suite.addTests(loader.loadTestsFromModule(test_module))
suite.run(self)
class ModuleStats(object): class ModuleStats(object):
errors = 0 errors = 0
success = 0 success = 0
failures = 0 failures = 0
time = 0.0 time = 0.0
modulesStats = {}
lastStart = None
def _getModuleStats(self, test): def _getModuleStats(self, test):
module = test.__class__.__name__ module = test.__class__.__name__
try: try:
...@@ -166,67 +190,54 @@ class NeoTestResult(unittest.TestResult): ...@@ -166,67 +190,54 @@ class NeoTestResult(unittest.TestResult):
self.summary = self._buildSummary() self.summary = self._buildSummary()
self.errors = self._buildErrors() self.errors = self._buildErrors()
def sendReport(self):
def sendReport(result): """ Send a mail with the report summary """
""" Send a mail with the report summary """
import smtplib
import smtplib from email.MIMEMultipart import MIMEMultipart
from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText
from email.MIMEText import MIMEText
# build the email
# build the email msg = MIMEMultipart()
msg = MIMEMultipart() msg['Subject'] = self.subject
msg['Subject'] = result.subject msg['From'] = SENDER
msg['From'] = SENDER msg['To'] = ', '.join(RECIPIENTS)
msg['To'] = ', '.join(RECIPIENTS) #msg.preamble = self.subject
#msg.preamble = result.subject msg.epilogue = ''
msg.epilogue = ''
# Add custom headers for client side filtering
# Add custom headers for client side filtering msg['X-ERP5-Tests'] = 'NEO'
msg['X-ERP5-Tests'] = 'NEO' if self.wasSuccessful():
if result.wasSuccessful(): msg['X-ERP5-Tests-Status'] = 'OK'
msg['X-ERP5-Tests-Status'] = 'OK'
# write the body
# write the body body = MIMEText(self.summary + self.errors, 'text')
body = MIMEText(result.summary + result.errors, 'text') msg.attach(body)
msg.attach(body)
# attach the log file
# attach the log file if ATTACH_LOG:
if ATTACH_LOG: log = MIMEText(file(LOG_FILE, 'r').read())
log = MIMEText(file(LOG_FILE, 'r').read()) log.add_header('Content-Disposition', 'attachment', filename=LOG_FILE)
log.add_header('Content-Disposition', 'attachment', filename=LOG_FILE) msg.attach(log)
msg.attach(log)
# Send the email via our own SMTP server.
# Send the email via our own SMTP server. s = smtplib.SMTP()
s = smtplib.SMTP() s.connect(*SMTP_SERVER)
s.connect(*SMTP_SERVER) mail = msg.as_string()
mail = msg.as_string() for recipient in RECIPIENTS:
for recipient in RECIPIENTS: s.sendmail(SENDER, recipient, mail)
s.sendmail(SENDER, recipient, mail) s.close()
s.close()
def buildTestSuite():
suite = unittest.TestSuite()
loader = unittest.defaultTestLoader
# load neo tests
for test_module in TEST_MODULES:
test_module = __import__(test_module, globals(), locals(), ['*'])
suite.addTests(loader.loadTestsFromModule(test_module))
# load ZODB tests
if WITH_ZODB_TESTS:
from neo.client.tests.testZODB import ZODBTests
suite.addTests(unittest.makeSuite(ZODBTests, 'check'))
return suite
if __name__ == "__main__": if __name__ == "__main__":
# run and build the report # run and build the report
result = NeoTestResult() runner = NeoTestRunner()
suite = buildTestSuite() runner.run('Unit tests', UNIT_TEST_MODULES)
suite.run(result) if WITH_ZODB_TESTS:
result.build() runner.run('Functional tests', FUNC_TEST_MODULES, prefix='check')
runner.build()
# send a mail # send a mail
if SEND_REPORT: if SEND_REPORT:
sendReport(result) runner.sendReport()
print result.summary print runner.errors
print result.errors print runner.summary
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment