-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathruntests.py
executable file
·72 lines (60 loc) · 2.04 KB
/
runtests.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
#! /usr/bin/env python
import sys
try:
import unittest2 as unittest
except ImportError:
import unittest
from os import path
from random import Random, randint
import coverage
class RandomOrderTestSuite(unittest.TestSuite):
"""
Test Suite that will randomize the order of tests. This avoids the tests
becoming dependent on some overlooked state. USE WITH CAUTION.
"""
def __init__(self, seed, *args, **kwargs):
if seed:
self.__seed = seed
else:
self.__seed = randint(0, 9999)
super(RandomOrderTestSuite, self).__init__(*args, **kwargs)
def __get_all_tests(self, test_case):
result = []
for item in test_case:
if hasattr(item, "_tests") and len(item._tests) > 0:
result += self.__get_all_tests(item)
else:
result.append(item)
return result
def run(self, result):
cases = self.__get_all_tests(self)
r = Random(self.__seed)
r.shuffle(cases)
for test in cases:
if result.shouldStop:
break
test(result)
print
print
print '>>> python runtests.py --seed={0}'.format(self.__seed)
return result
if __name__ == "__main__":
cov = coverage.coverage(source=[path.join(path.dirname(__file__), 'datatree')])
cov.start()
seed = None
for arg in sys.argv:
if arg.startswith('--seed='):
seed = int(arg.split('=')[1])
current_folder = path.dirname(__file__)
base_folder = path.join(current_folder, "datatree")
sys.path.insert(0, current_folder)
suite = RandomOrderTestSuite(seed)
loader = unittest.loader.defaultTestLoader
suite.addTest(loader.discover(base_folder, pattern="test*.py"))
runner = unittest.TextTestRunner()
runner.verbosity = 2
runner.run(suite.run)
cov.stop()
# Output the coverage
cov.html_report(directory='htmlcov')
print 'Coverage report written to: {0}'.format(path.join(path.dirname(__file__), 'htmlcov', 'index.html'))