-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_tests.py
executable file
·114 lines (98 loc) · 3.71 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
#!/usr/bin/env python
import os
import sys
import optparse
import django
import django.conf
import django.test.utils
__author__ = 'Tomasz J. Kotarba <[email protected]>'
__copyright__ = 'Copyright (c) 2016, Tomasz J. Kotarba. All rights reserved.'
__maintainer__ = 'Tomasz J. Kotarba'
__email__ = '[email protected]'
def parse_command_line_arguments():
usage = "%prog [options] [module module module ...]"
parser = optparse.OptionParser(usage=usage)
parser.add_option('-v', '--verbosity', action='store', dest='verbosity',
default=1, type='choice', choices=['0', '1', '2', '3'],
help='Verbosity level; 0=minimal output, 1=normal '
'output, 2=all output')
parser.add_option('--noinput', action='store_false', dest='interactive',
default=True,
help='Tells Django to NOT prompt the user for input of '
'any kind.')
parser.add_option('--settings',
help='Python path to settings module, e.g. '
'"myproject.settings". If this isn\'t provided, '
'the DJANGO_SETTINGS_MODULE environment variable '
'will be used.')
options, arguments = parser.parse_args()
if options.settings:
os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
elif "DJANGO_SETTINGS_MODULE" in os.environ:
options.settings = os.environ['DJANGO_SETTINGS_MODULE']
return options, arguments
def configure_django_settings():
django.conf.settings.configure(
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
}
},
INSTALLED_APPS=(
'rapidsms',
'rapidpro4rapidsms',
),
SITE_ID=1,
SECRET_KEY='the-password',
LOGGING={
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'null': {
'level': 'DEBUG',
'class': 'django.utils.log.NullHandler',
},
},
'loggers': {
'rapidsms': {
'handlers': ['null'],
'level': 'DEBUG',
},
'rapidpro4rapidsms': {
'handlers': ['null'],
'level': 'DEBUG',
}
}
},
ROOT_URLCONF='rapidpro4rapidsms.urls',
INSTALLED_BACKENDS={
"rapidpro-backend": {
"ENGINE": "rapidpro4rapidsms.RapidProBackend",
"rapidpro_api_gateway_url": "http://127.0.0.1:8080/api/v1",
"rapidpro_api_token": "YOUR-RAPIDPRO-API-TOKEN"
}
}
)
def prepare_django_test_runner(options):
if not options.settings:
configure_django_settings()
if django.VERSION > (1, 7):
# django.readthedocs.org/en/latest/releases/1.7.html#standalone-scripts
django.setup()
test_runner_class = django.test.utils.get_runner(django.conf.settings)
test_runner = test_runner_class(verbosity=int(options.verbosity),
interactive=options.interactive,
failfast=False)
return test_runner
def run_tests(test_runner, arguments):
if not arguments or arguments[0] == 'test':
arguments = ['tests']
failures = test_runner.run_tests(arguments)
sys.exit(failures)
def main():
options, arguments = parse_command_line_arguments()
test_runner = prepare_django_test_runner(options)
run_tests(test_runner, arguments)
if __name__ == '__main__':
main()