-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_tests.py
executable file
·59 lines (47 loc) · 1.7 KB
/
run_tests.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
#!/usr/bin/env python
import sys
#HACK: This will use the unittest backport from Python 2.7 to be able of use:
# unittest.TestLoader().discover()
if sys.version_info[0] == 2 and sys.version_info[1] == 6:
import unittest2 as unittest
else:
import unittest
import os
import fnmatch
def get_test_dirs(path):
"""get_test_dirs(path) => list
Returns a list of direcotries and subdirectories with Pyton tests.
"""
test_dirs = []
for root, dirs, files in os.walk(path):
for filename in files:
if fnmatch.fnmatch(filename, 'test_*.py'):
test_dirs.append(root)
break
return test_dirs
def run_tests_in(test_dirs=['.']):
"""run_tests_in(path) => True or False
Run all the tests until they end or one fails. It returns True if
all the tests pass and False if any of them fails.
"""
for test_dir in test_dirs:
sys.stderr.write("Testing: %s\n" % test_dir)
test_loader = unittest.TestLoader()
test_suite = test_loader.discover(test_dir)
test_runner = unittest.TextTestRunner(verbosity=2)
result = test_runner.run(test_suite)
if not result.wasSuccessful():
return False
return True
if __name__ == '__main__':
# It can take one directory as argument, but only one.
# If zero,more than one agrument are passed or the argument isn't a valid
# directory, it will use the script's directory.
if len(sys.argv) == 2 and os.path.isdir(sys.argv[1]):
root_dir = sys.argv[1]
else:
root_dir = os.path.dirname(__file__)
test_dirs = get_test_dirs(root_dir)
was_successful = run_tests_in(test_dirs)
if not was_successful:
sys.exit(1)