Skip to content

Commit 9e8fefe

Browse files
author
Johannes Gijsbers
committed
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).
1 parent d297ddb commit 9e8fefe

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

test/__init__.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: __init__.py,v 1.18 2002-09-10 00:19:54 richard Exp $
18+
# $Id: __init__.py,v 1.19 2003-09-07 20:37:33 jlgijsbers Exp $
1919

20-
import os, tempfile, unittest, shutil
20+
import os, tempfile, unittest, shutil, errno
2121
import roundup.roundupdb
2222
roundup.roundupdb.SENDMAILDEBUG=os.environ['SENDMAILDEBUG']=tempfile.mktemp()
2323

24+
from roundup import init
25+
2426
# figure all the modules available
2527
dir = os.path.split(__file__)[0]
2628
test_mods = {}
@@ -30,12 +32,36 @@
3032
test_mods[name] = __import__(file[:-3], globals(), locals(), [])
3133
all_tests = test_mods.keys()
3234

35+
dirname = '_empty_instance'
36+
def create_empty_instance():
37+
remove_empty_instance()
38+
init.install(dirname, 'templates/classic')
39+
init.write_select_db(dirname, 'anydbm')
40+
init.initialise(dirname, 'sekrit')
41+
42+
def remove_empty_instance():
43+
try:
44+
shutil.rmtree(dirname)
45+
except OSError, error:
46+
if error.errno not in (errno.ENOENT, errno.ESRCH): raise
47+
3348
def go(tests=all_tests):
34-
l = []
35-
for name in tests:
36-
l.append(test_mods[name].suite())
37-
suite = unittest.TestSuite(l)
38-
runner = unittest.TextTestRunner()
39-
runner.run(suite)
49+
try:
50+
l = []
51+
needs_instance = 0
52+
for name in tests:
53+
mod = test_mods[name]
54+
if hasattr(mod, 'NEEDS_INSTANCE'):
55+
needs_instance = 1
56+
l.append(test_mods[name].suite())
57+
58+
if needs_instance:
59+
create_empty_instance()
60+
61+
suite = unittest.TestSuite(l)
62+
runner = unittest.TextTestRunner()
63+
runner.run(suite)
64+
finally:
65+
remove_empty_instance()
4066

4167
# vim: set filetype=python ts=4 sw=4 et si

test/test_cgi.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
# but WITHOUT ANY WARRANTY; without even the implied warranty of
99
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1010
#
11-
# $Id: test_cgi.py,v 1.18 2003-08-11 11:28:31 jlgijsbers Exp $
11+
# $Id: test_cgi.py,v 1.19 2003-09-07 20:37:33 jlgijsbers Exp $
1212

1313
import unittest, os, shutil, errno, sys, difflib, cgi, re
1414

1515
from roundup.cgi import client
1616
from roundup import init, instance, password, hyperdb, date
1717

18+
NEEDS_INSTANCE = 1
19+
1820
class FileUpload:
1921
def __init__(self, content, filename):
2022
self.content = content
@@ -65,9 +67,8 @@ def setUp(self):
6567
except OSError, error:
6668
if error.errno not in (errno.ENOENT, errno.ESRCH): raise
6769
# create the instance
68-
init.install(self.dirname, 'templates/classic')
69-
init.write_select_db(self.dirname, 'anydbm')
70-
init.initialise(self.dirname, 'sekrit')
70+
shutil.copytree('_empty_instance', self.dirname)
71+
7172
# check we can load the package
7273
self.instance = instance.open(self.dirname)
7374
# and open the database

test/test_mailgw.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# but WITHOUT ANY WARRANTY; without even the implied warranty of
99
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1010
#
11-
# $Id: test_mailgw.py,v 1.50 2003-09-07 18:27:47 jlgijsbers Exp $
11+
# $Id: test_mailgw.py,v 1.51 2003-09-07 20:37:33 jlgijsbers Exp $
1212

1313
import unittest, tempfile, os, shutil, errno, imp, sys, difflib, rfc822
1414

@@ -17,6 +17,8 @@
1717
from roundup.mailgw import MailGW, Unauthorized, uidFromAddress
1818
from roundup import init, instance, rfc2822
1919

20+
NEEDS_INSTANCE = 1
21+
2022
class Message(rfc822.Message):
2123
"""String-based Message class with equivalence test."""
2224
def __init__(self, s):
@@ -77,9 +79,8 @@ def setUp(self):
7779
except OSError, error:
7880
if error.errno not in (errno.ENOENT, errno.ESRCH): raise
7981
# create the instance
80-
init.install(self.dirname, 'templates/classic')
81-
init.write_select_db(self.dirname, 'anydbm')
82-
init.initialise(self.dirname, 'sekrit')
82+
shutil.copytree('_empty_instance', self.dirname)
83+
8384
# check we can load the package
8485
self.instance = instance.open(self.dirname)
8586
# and open the database

0 commit comments

Comments
 (0)