forked from cobrateam/splinter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_tests.py
executable file
·143 lines (104 loc) · 3.36 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import sys
try:
import unittest2 as unittest
except ImportError, e:
import unittest
from multiprocessing import Process
from urllib import urlopen
from tests import TESTS_ROOT
from tests.fake_webapp import start_flask_app, EXAMPLE_APP
parser = argparse.ArgumentParser('Run splinter tests')
parser.add_argument('-w', '--which', action='store')
class Env(object):
pass
env = Env()
env.process = None
env.host, env.port = 'localhost', 5000
def wait_until_start():
while True:
try:
urlopen(EXAMPLE_APP)
break
except IOError:
pass
def wait_until_stop():
while True:
try:
results = urlopen(EXAMPLE_APP)
if results.code == 404:
break
except IOError:
break
def start_server():
sys.stderr = open('/dev/null', 'w')
env.process = Process(target=start_flask_app, args=(env.host, env.port))
env.process.daemon = True
env.process.start()
wait_until_start()
def stop_server():
env.process.terminate()
env.process.join()
wait_until_stop()
def get_modules(modules_str):
names = modules_str.split(',')
modules = []
for name in names:
name = name.replace('/', '.').replace('.py', '')
try:
module = __import__(name, fromlist='tests')
except:
exc_type, exc_value, exc_traceback = sys.exc_info()
print 'Error importing module %s:' % name
import traceback
traceback.print_exception(exc_type, exc_value, exc_traceback,
file=sys.stdout)
modules.append(module)
return modules
def run_suite(suite):
result = unittest.TextTestResult(sys.stdout, descriptions=True, verbosity=1)
suite.run(result)
sys.stdout.write("\n\n")
return result
def get_suite_from_modules(modules):
loader = unittest.TestLoader()
suite = unittest.TestSuite()
for module in modules:
suite.addTest(loader.loadTestsFromModule(module))
return suite
def get_complete_suite():
loader = unittest.TestLoader()
return loader.discover(TESTS_ROOT)
def print_errors(result):
if result.errors:
sys.stdout.write("\nERRORS\n\n")
for method, trace in result.errors:
sys.stdout.write("Test method: %s\n" % method)
sys.stdout.write("%s" % trace)
sys.stdout.write("=" * 120)
sys.stdout.write("\n\n")
def print_failures(result):
if result.failures:
sys.stdout.write("\nFAILURES\n\n")
for method, trace in result.failures:
sys.stdout.write("Test method: %s\n" % method)
sys.stdout.write("%s" % trace)
sys.stdout.write("=" * 120)
sys.stdout.write("\n\n")
if __name__ == '__main__':
start_server()
args = parser.parse_args()
loader = unittest.TestLoader()
if args.which and args.which != 'tests':
modules = get_modules(args.which)
suite = get_suite_from_modules(modules)
else:
suite = get_complete_suite()
result = run_suite(suite)
print_failures(result)
print_errors(result)
sys.stdout.write("%d tests. %d failures. %d errors.\n\n" % (result.testsRun, len(result.failures), len(result.errors)))
stop_server()
sys.exit(not result.wasSuccessful())