FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cefpython/unittests/_test_runner.py at cefpython57 · pythonthings/cefpython · GitHub
pythonthings
cefpython
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cefpython
/
unittests
/
_test_runner.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
286 lines (255 loc) · 10.5 KB
Breadcrumbs
cefpython
/
unittests
/
_test_runner.py
Copy path
File metadata and controls
286 lines (255 loc) · 10.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# Copyright (c) 2016 CEF Python, see the Authors file.
# All rights reserved. Licensed under BSD 3-clause license.
# Project website: https://github.com/cztomczak/cefpython
"""Run unit tests. With no arguments all tests are run. Read notes below.
Usage:
_test_runner.py [--debug] [FILE | _TESTCASE]
Options:
--debug Enable debug info
FILE Run tests from single file
_TESTCASE Test cases matching pattern to run eg "file.TestCase".
Calling with this argument is for internal use only.
It has side effects, so don't use it. See comments.
Notes:
- Files starting with "_" are ignored
- If test case name contains "IsolatedTest" word then this test
case will be run using a new instance of Python interpreter.
In such case instead of calling "unittest.main()" use this code:
"import _runner; _runner.main(os.path.basename(__file__))".
- Tested only with TestCase objects. TestSuite usage is untested.
"""
import
unittest
import
os
import
platform
import
sys
from
os
.
path
import
dirname
,
realpath
import
re
import
subprocess
# Command line args
CUSTOM_CMDLINE_ARG
=
""
def
main
(
file_arg
=
""
):
# type: (str) -> None
"""Main entry point."""
# Set working dir to script's current
os
.
chdir
(
dirname
(
realpath
(
__file__
)))
# Script arguments
testcase_arg
=
""
if
len
(
sys
.
argv
)
>
1
and
sys
.
argv
[
1
].
startswith
(
"--"
):
# Will allow to pass custom args like --debug to isolated tests
# (main_test.py for example).
global
CUSTOM_CMDLINE_ARG
CUSTOM_CMDLINE_ARG
=
sys
.
argv
[
1
]
elif
len
(
sys
.
argv
)
>
1
:
if
".py"
in
sys
.
argv
[
1
]:
file_arg
=
sys
.
argv
[
1
]
else
:
testcase_arg
=
sys
.
argv
[
1
]
# Run tests
runner
=
TestRunner
()
if
testcase_arg
:
runner
.
run_testcase
(
testcase_arg
)
elif
file_arg
:
runner
.
run_file
(
file_arg
)
else
:
runner
.
run_all
()
class
TestRunner
(
object
):
"""Customized test runner."""
ran
=
0
errors
=
0
failures
=
0
cefpython_version
=
"-unknown-"
_suites
=
None
# type: unittest.TestSuite
_isolated_suites
=
None
# type: unittest.TestSuite
_import_errors
=
None
# type: unittest.TestSuite
def
_reset_state
(
self
):
# type: () -> None
"""Reset TestRunner state before test discovery."""
self
.
ran
=
0
self
.
errors
=
0
self
.
failures
=
0
self
.
_suites
=
unittest
.
TestSuite
()
self
.
_isolated_suites
=
unittest
.
TestSuite
()
self
.
_import_errors
=
unittest
.
TestSuite
()
# ---- Public methods
def
run_testcase
(
self
,
testcase
):
# type: (str) -> None
"""Run single test case eg 'foo.BarTest'. This is needed to
run single testcase that is marked as IsolatedTest."""
self
.
_discover
(
"[!_]*.py"
,
testcase
)
assert
not
self
.
_count_suites
(
self
.
_isolated_suites
)
if
not
self
.
_count_suites
(
self
.
_suites
):
print
(
"[_test_runner.py] ERROR: test case not found"
)
sys
.
exit
(
1
)
# Import errors found during discovery are ignored
self
.
_run_suites
(
self
.
_suites
)
self
.
_exit
()
def
run_file
(
self
,
filename
):
# type: (str) -> None
"""Run test cases from a specific file. This is needed so that
you can use _runner.main() in isolated tests."""
self
.
_discover
(
filename
)
self
.
_run_discovered_suites
()
def
run_all
(
self
):
# type: () -> None
"""Run all tests from current directory."""
self
.
_discover
(
"[!_]*.py"
)
self
.
_run_discovered_suites
()
# ---- Private methods
def
_run_discovered_suites
(
self
):
# type: () -> None
"""Run both normal and isolated suites."""
suites
=
self
.
_merge_suites
(
self
.
_import_errors
,
self
.
_suites
)
self
.
_run_suites
(
suites
)
self
.
_run_suites_in_isolation
(
self
.
_isolated_suites
)
self
.
_print_summary
()
def
_run_suites
(
self
,
suites
):
# type: (unittest.TestSuite) -> None
"""Run suites."""
if
not
self
.
_count_suites
(
suites
):
return
runner
=
unittest
.
TextTestRunner
(
verbosity
=
2
,
descriptions
=
True
,
buffer
=
False
)
# Update "ran" before running suites, because after ran
# counting them doesn't work (Python 3 issue).
self
.
ran
+=
self
.
_count_suites
(
suites
)
result
=
runner
.
run
(
suites
)
self
.
errors
+=
len
(
result
.
errors
)
self
.
failures
+=
len
(
result
.
failures
)
def
_run_suites_in_isolation
(
self
,
suites
):
# type: (unittest.TestSuite) -> None
"""Run each suite using new instance of Python interpreter."""
if
not
self
.
_count_suites
(
suites
):
return
for
suite
in
suites
:
# Find test case identifier
testcase_id
=
""
for
testcase
in
suite
:
testcase_id
=
testcase
.
id
()
break
# Run test using new instance of Python interpreter
try
:
output
=
subprocess
.
check_output
(
[
sys
.
executable
,
"_test_runner.py"
,
testcase_id
,
CUSTOM_CMDLINE_ARG
],
stderr
=
subprocess
.
STDOUT
)
exit_code
=
0
except
subprocess
.
CalledProcessError
as
exc
:
output
=
exc
.
output
exit_code
=
exc
.
returncode
if
type
(
output
)
!=
str
:
output
=
output
.
decode
(
"utf-8"
,
errors
=
"replace"
)
# Fetch number of sub-tests ran from output
match
=
re
.
search
(
r"^Ran (\d+) sub-tests in \w+"
,
output
,
re
.
MULTILINE
)
if
match
:
self
.
ran
+=
int
(
match
.
group
(
1
))
# Fetch CEF Python version from output
match
=
re
.
search
(
r"^CEF Python (\d+\.\d+)"
,
output
,
re
.
MULTILINE
)
if
match
:
self
.
cefpython_version
=
match
.
group
(
1
)
# Write original output
sys
.
stdout
.
write
(
output
)
# If tests failed parse output for errors/failures
if
exit_code
:
if
output
:
lines
=
output
.
splitlines
()
lastline
=
lines
[
len
(
lines
)
-
1
]
match
=
re
.
search
(
r"failures=(\d+)"
,
lastline
)
if
match
:
self
.
failures
+=
int
(
match
.
group
(
1
))
match
=
re
.
search
(
r"errors=(\d+)"
,
lastline
)
if
match
:
self
.
errors
+=
int
(
match
.
group
(
1
))
if
not
self
.
errors
and
not
self
.
failures
:
self
.
errors
+=
1
elif
output
:
# Test case still might have failed and unittest would not
# detect this. For example when assertion fails
# in ClientHandler in core_test.py .
if
"Traceback (most recent call last)"
in
output
\
or
"AssertionError"
in
output
:
self
.
errors
+=
1
# Update ran
self
.
ran
+=
self
.
_count_suites
(
suites
)
def
_count_suites
(
self
,
suites
):
# type: (unittest.TestSuite) -> int
count
=
0
for
suite
in
suites
:
if
isinstance
(
suite
,
unittest
.
TestSuite
):
for
_
in
suite
:
count
+=
1
return
count
def
_merge_suites
(
self
,
suites1
,
suites2
):
# type: (unittest.TestSuite, unittest.TestSuite) -> unittest.TestSuite
merged
=
unittest
.
TestSuite
()
for
suite
in
suites1
:
merged
.
addTest
(
suite
)
for
suite
in
suites2
:
merged
.
addTest
(
suite
)
return
merged
def
_discover
(
self
,
pattern
,
testcase_name
=
""
):
# type: (str, str) -> None
"""Test discovery using glob pattern from arg or main()."""
self
.
_reset_state
()
loader
=
unittest
.
TestLoader
()
discovered_suite
=
loader
.
discover
(
start_dir
=
"."
,
pattern
=
pattern
)
for
level1_suite
in
discovered_suite
:
for
level2_suite
in
level1_suite
:
if
isinstance
(
level2_suite
,
unittest
.
TestSuite
):
for
testcase_obj
in
level2_suite
:
if
testcase_name
:
if
re
.
match
(
re
.
escape
(
testcase_name
),
testcase_obj
.
id
()):
self
.
_suites
.
addTest
(
level2_suite
)
break
elif
"IsolatedTest"
in
testcase_obj
.
id
():
self
.
_isolated_suites
.
addTest
(
level2_suite
)
break
else
:
self
.
_suites
.
addTest
(
level2_suite
)
break
elif
not
testcase_name
:
# unittest.loader.ModuleImportFailure
# Warning: If there is an import error in a file
# containing a test case that is being run then there
# won't be any error displayed about it when running
# that single test case. However running a single test
# case is for internal use only, to run test cases in
# isolation with a new instance of Python interpreter.
# Import errors will always be showed when running all
# tests or tests from file.
self
.
_import_errors
.
addTest
(
level2_suite
)
def
_print_summary
(
self
):
# type: () -> None
"""Print summary and exit."""
print
(
"-"
*
70
)
print
(
"[_test_runner.py] CEF Python {ver}"
.
format
(
ver
=
self
.
cefpython_version
))
print
(
"[_test_runner.py] Python {ver} {arch}"
.
format
(
ver
=
platform
.
python_version
(),
arch
=
platform
.
architecture
()[
0
]))
print
(
"[_test_runner.py] Ran {ran} tests in total"
.
format
(
ran
=
self
.
ran
))
if
self
.
errors
or
self
.
failures
:
failed_str
=
"[_test_runner.py] FAILED ("
if
self
.
failures
:
failed_str
+=
(
"failures="
+
str
(
self
.
failures
))
if
self
.
errors
:
if
self
.
failures
:
failed_str
+=
", "
failed_str
+=
(
"errors="
+
str
(
self
.
errors
))
failed_str
+=
")"
print
(
failed_str
)
else
:
print
(
"[_test_runner.py] OK all unit tests succeeded"
)
self
.
_exit
()
def
_exit
(
self
):
# type: () -> None
"""Exit with appropriate exit code."""
if
self
.
errors
or
self
.
failures
:
sys
.
exit
(
1
)
else
:
sys
.
exit
(
0
)
if
__name__
==
"__main__"
:
main
()
Back
|
FazBrowse Home
|
New Git URL