forked from crossgovernmentservices/ags_gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-tests
executable file
·91 lines (62 loc) · 1.88 KB
/
run-tests
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
#!/usr/bin/env python3
import argparse
from lib.govuk_assets import install_govuk_assets
BASE_TEST_RUNNER_ARGS = [
'--eradicate',
'--flake8',
'--spec',
]
def main():
args = get_args()
try:
return run_tests(args.test, watch=args.watch, coverage=args.coverage)
except KeyboardInterrupt:
pass
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--coverage', action='store_true', help="""
Generate coverage report""".strip())
parser.add_argument('-t', '--test', default='tests', help="""
The test module to run""".strip())
parser.add_argument('-w', '--watch', action='store_true', help="""
Watch files for changes and rerun tests automatically""".strip())
return parser.parse_args()
def run_tests(suite, watch=False, coverage=False):
from lib.script_utils import virtualenv
with virtualenv():
args = build_args(suite, coverage)
test_runner = run_on_updates() if watch else run_once()
return test_runner(args)
def build_args(suite, coverage=False):
args = BASE_TEST_RUNNER_ARGS
args.extend(specified_test_suite(suite))
args.extend(coverage_report(coverage))
return args
def specified_test_suite(suite):
if not suite:
suite = 'tests'
return ['--pyargs', suite]
def coverage_report(coverage):
if coverage:
return [
'--cov-report', 'term',
'--cov-report', 'xml',
'--cov=app',
]
return []
def run_on_updates():
import pytest_watch
def runner(args):
watch_args = [
'--ignore', 'venv',
'--'
]
return pytest_watch.command.main(watch_args + args)
return runner
def run_once():
import pytest
return pytest.main
if __name__ == '__main__':
import sys
install_govuk_assets('app')
sys.exit(main())