-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest.py
95 lines (81 loc) · 3.04 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Gauged - https://github.com/chriso/gauged (MIT Licensed)
Copyright 2014 (c) Chris O'Hara <[email protected]>
"""
import unittest
import sys
import gc
from ConfigParser import ConfigParser
import gauged
from test import (TestGauged, TestStructures, TestLRU, TestDriver,
TestDSN, TestResult)
# Get the list of test drivers
config = ConfigParser()
config.read('test_drivers.cfg')
test_drivers = config.items('drivers')
# Parse command line options
argv = set(sys.argv)
# Use only the first driver (in-memory SQLite)
quick_tests = '--quick' in argv
# Stop and dump a trace when a test driver fails
raise_on_error = '--raise' in argv
# Run the tests forever (to check for leaks)
run_forever = '--forever' in argv
# Run driver tests only
drivers_only = '--drivers' in argv
# Increase test runner verbosity
verbose = '--verbose' in argv
if quick_tests:
test_drivers = test_drivers[:1]
def run_tests():
suite = unittest.TestSuite()
test_class = driver = None
# Test each driver in gauged/drivers. We need to dynamically subclass
# the TestDriver class here - the unittest module doesn't allow us to
# use __init__ to pass in driver arguments or a driver instance
for driver, dsn in test_drivers:
try:
gauged_instance = gauged.Gauged(dsn)
# Empty the data store
gauged_instance.driver.drop_schema()
gauged_instance.sync()
gauged_instance.driver.create_schema()
# Test the driver class
test_class = type('Test%s' % driver, (TestDriver,), {})
test_class.driver = gauged_instance.driver
suite.addTest(unittest.makeSuite(test_class))
# Test gauged/gauged.py using the driver
if not drivers_only:
test_class = type('TestGaugedWith%s' % driver,
(TestGauged,), {})
test_class.driver = gauged_instance.driver
suite.addTest(unittest.makeSuite(test_class))
except Exception as e: # pylint: disable=broad-except
print 'Skipping %s tests (%s). Check test_driver.cfg' % \
(driver[:-6], str(e).rstrip())
if raise_on_error:
raise
if test_class is None:
raise RuntimeWarning('No drivers available for unit tests, check '
'configuration in test_driver.cfg')
# Test the remaining classes
if not drivers_only:
suite.addTest(unittest.makeSuite(TestStructures))
suite.addTest(unittest.makeSuite(TestLRU))
suite.addTest(unittest.makeSuite(TestDSN))
suite.addTest(unittest.makeSuite(TestResult))
# Setup the test runner
verbosity = 2 if verbose else 1
if run_forever:
verbosity = 0
test_runner = unittest.TextTestRunner(verbosity=verbosity)
# Run the tests
while True:
result = test_runner.run(suite)
if result.errors or result.failures:
exit(1)
if not run_forever:
break
gc.collect()
if __name__ == '__main__':
run_tests()