This repository has been archived by the owner on Aug 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
tests.py
95 lines (84 loc) · 2.81 KB
/
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
#!/usr/bin/env python
from sandbox import HAVE_CSANDBOX
from sys import version_info
from sandbox.test import SkipTest, createSandboxConfig
from sandbox.test.tools import getTests
from sandbox.version import VERSION
def parseOptions():
from optparse import OptionParser
parser = OptionParser(usage="%prog [options]")
parser.add_option("--raise",
help="Don't catch exception",
dest="raise_exception",
action="store_true")
parser.add_option("--debug",
help="Enable debug mode (enable stdout and stderr features)",
action="store_true")
parser.add_option("-k", "--keyword",
help="Only execute tests with name containing KEYWORD",
type='str')
options, argv = parser.parse_args()
if argv:
parser.print_help()
exit(1)
return options
def run_tests(options, use_subprocess, cpython_restricted):
print("Run tests with cpython_restricted=%s and use_subprocess=%s"
% (cpython_restricted, use_subprocess))
print("")
createSandboxConfig.cpython_restricted = cpython_restricted
createSandboxConfig.use_subprocess = use_subprocess
# Get all tests
all_tests = getTests(globals(), options.keyword)
# Run tests
nerror = 0
nskipped = 0
if version_info < (2, 6):
base_exception = Exception
else:
base_exception = BaseException
for func in all_tests:
name = '%s.%s()' % (func.__module__.split('.')[-1], func.__name__)
if options.debug:
print(name)
try:
func()
except SkipTest, skip:
print("%s: skipped (%s)" % (name, skip))
nskipped += 1
except base_exception, err:
nerror += 1
print("%s: FAILED! %r" % (name, err))
if options.raise_exception:
raise
else:
print "%s: ok" % name
print("")
return nskipped, nerror, len(all_tests)
def main():
options = parseOptions()
createSandboxConfig.debug = options.debug
print("Run the test suite on pysandbox %s with Python %s.%s"
% (VERSION, version_info[0], version_info[1]))
if not HAVE_CSANDBOX:
print("WARNING: _sandbox module is missing")
print
nskipped, nerrors, ntests = 0, 0, 0
for use_subprocess in (False, True):
for cpython_restricted in (False, True):
result = run_tests(options, use_subprocess, cpython_restricted)
nskipped += result[0]
nerrors += result[1]
ntests += result[2]
if options.raise_exception and nerrors:
break
# Exit
from sys import exit
if nerrors:
print("%s ERRORS!" % nerrors)
exit(1)
else:
print("%s tests succeed (%s skipped)" % (ntests, nskipped))
exit(0)
if __name__ == "__main__":
main()