forked from cobrateam/splinter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_tests.py
executable file
·134 lines (99 loc) · 3.22 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
#!/usr/bin/env python
# Copyright 2013 splinter authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
# -*- coding: utf-8 -*-
import argparse
import sys
import unittest
import os
from multiprocessing import Process
try:
from urllib import urlopen
except ImportError:
from urllib.request 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')
parser.add_argument('-f', '--failfast', action='store_true')
parser.add_argument('-v', '--verbosity', type=int, default=1)
class Env(object):
pass
env = Env()
env.process = None
env.host, env.port = 'localhost', 5000
def wait_until_start():
while True:
try:
results = urlopen(EXAMPLE_APP)
if results.code == 404:
raise Exception('%s returned unexpected 404' % 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 {}:'.format(name))
import traceback
traceback.print_exception(exc_type, exc_value, exc_traceback,
file=sys.stdout)
modules.append(module)
return modules
def run_suite(suite, args):
runner = unittest.TextTestRunner(sys.stdout, True, args.verbosity,
args.failfast)
return runner.run(suite)
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(
start_dir=TESTS_ROOT,
top_level_dir=os.path.join(TESTS_ROOT, os.path.pardir)
)
if __name__ == '__main__':
try:
start_server()
except Exception as e:
sys.stdout.write("Failed to start test server: %s\n\n" % e)
sys.exit(1)
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, args)
stop_server()
sys.exit(len(result.errors) + len(result.failures))