From: jlgijsbers Date: Sun, 7 Sep 2003 20:37:33 +0000 (+0000) Subject: Optimize mailgw and cgi tests by creating an empty instance before the tests X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=15d080f7d66886bee97ebed30d81ed5ef1473cee;p=roundup.git Optimize mailgw and cgi tests by creating an empty instance before the tests start (only if needed). In setUp(), this instance is then copied to another directory for the actual tests. On my system, this about halved the execution time for test_cgi (33s -> 14s) and test_mailgw (40s -> 25s). git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/trunk@1864 57a73879-2fb5-44c3-a270-3262357dd7e2 --- diff --git a/test/__init__.py b/test/__init__.py index 5273971..90d0f01 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -15,12 +15,14 @@ # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # -# $Id: __init__.py,v 1.18 2002-09-10 00:19:54 richard Exp $ +# $Id: __init__.py,v 1.19 2003-09-07 20:37:33 jlgijsbers Exp $ -import os, tempfile, unittest, shutil +import os, tempfile, unittest, shutil, errno import roundup.roundupdb roundup.roundupdb.SENDMAILDEBUG=os.environ['SENDMAILDEBUG']=tempfile.mktemp() +from roundup import init + # figure all the modules available dir = os.path.split(__file__)[0] test_mods = {} @@ -30,12 +32,36 @@ for file in os.listdir(dir): test_mods[name] = __import__(file[:-3], globals(), locals(), []) all_tests = test_mods.keys() +dirname = '_empty_instance' +def create_empty_instance(): + remove_empty_instance() + init.install(dirname, 'templates/classic') + init.write_select_db(dirname, 'anydbm') + init.initialise(dirname, 'sekrit') + +def remove_empty_instance(): + try: + shutil.rmtree(dirname) + except OSError, error: + if error.errno not in (errno.ENOENT, errno.ESRCH): raise + def go(tests=all_tests): - l = [] - for name in tests: - l.append(test_mods[name].suite()) - suite = unittest.TestSuite(l) - runner = unittest.TextTestRunner() - runner.run(suite) + try: + l = [] + needs_instance = 0 + for name in tests: + mod = test_mods[name] + if hasattr(mod, 'NEEDS_INSTANCE'): + needs_instance = 1 + l.append(test_mods[name].suite()) + + if needs_instance: + create_empty_instance() + + suite = unittest.TestSuite(l) + runner = unittest.TextTestRunner() + runner.run(suite) + finally: + remove_empty_instance() # vim: set filetype=python ts=4 sw=4 et si diff --git a/test/test_cgi.py b/test/test_cgi.py index db6081c..0a56025 100644 --- a/test/test_cgi.py +++ b/test/test_cgi.py @@ -8,13 +8,15 @@ # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # -# $Id: test_cgi.py,v 1.18 2003-08-11 11:28:31 jlgijsbers Exp $ +# $Id: test_cgi.py,v 1.19 2003-09-07 20:37:33 jlgijsbers Exp $ import unittest, os, shutil, errno, sys, difflib, cgi, re from roundup.cgi import client from roundup import init, instance, password, hyperdb, date +NEEDS_INSTANCE = 1 + class FileUpload: def __init__(self, content, filename): self.content = content @@ -65,9 +67,8 @@ class FormTestCase(unittest.TestCase): except OSError, error: if error.errno not in (errno.ENOENT, errno.ESRCH): raise # create the instance - init.install(self.dirname, 'templates/classic') - init.write_select_db(self.dirname, 'anydbm') - init.initialise(self.dirname, 'sekrit') + shutil.copytree('_empty_instance', self.dirname) + # check we can load the package self.instance = instance.open(self.dirname) # and open the database diff --git a/test/test_mailgw.py b/test/test_mailgw.py index 2fce57d..13cea7a 100644 --- a/test/test_mailgw.py +++ b/test/test_mailgw.py @@ -8,7 +8,7 @@ # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # -# $Id: test_mailgw.py,v 1.50 2003-09-07 18:27:47 jlgijsbers Exp $ +# $Id: test_mailgw.py,v 1.51 2003-09-07 20:37:33 jlgijsbers Exp $ import unittest, tempfile, os, shutil, errno, imp, sys, difflib, rfc822 @@ -17,6 +17,8 @@ from cStringIO import StringIO from roundup.mailgw import MailGW, Unauthorized, uidFromAddress from roundup import init, instance, rfc2822 +NEEDS_INSTANCE = 1 + class Message(rfc822.Message): """String-based Message class with equivalence test.""" def __init__(self, s): @@ -77,9 +79,8 @@ class MailgwTestCase(unittest.TestCase, DiffHelper): except OSError, error: if error.errno not in (errno.ENOENT, errno.ESRCH): raise # create the instance - init.install(self.dirname, 'templates/classic') - init.write_select_db(self.dirname, 'anydbm') - init.initialise(self.dirname, 'sekrit') + shutil.copytree('_empty_instance', self.dirname) + # check we can load the package self.instance = instance.open(self.dirname) # and open the database