-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_unit_tests.py
128 lines (93 loc) · 3.39 KB
/
run_unit_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
#!/usr/bin/python2.4
#
# Copyright 2010 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ========================================================================
"""Runs a set of unit tests and returns success only if they all succeed.
This script assumes it is being run from the omaha directory.
To run unit tests for Omaha's default set of test directories, just run the file
from the command line.
"""
import dircache
import os
TEST_EXECUTABLE_RHS = '_unittest.exe'
# Build paths that contain tests.
STAGING_PATH = 'scons-out\\dbg-win\\staging'
TESTS_PATH = 'scons-out\\dbg-win\\tests'
def RunTest(test_path):
"""Runs a test and returns its exit code.
Assumes the tests can be run from any directory. In other words, it does not
chdir.
Args:
test_path: Path to test executables.
Returns:
The exit code from the test process.
"""
print '\nRunning %s . . .\n' % test_path
# Put './' in front of the file name to avoid accidentally running a file with
# the same name in some other directory if test_path were just a file name.
return os.system(os.path.join('.', test_path))
def RunTests(test_paths):
"""Runs all tests specified by test_paths.
Args:
test_paths: A list of paths to test executables.
Returns:
0 if all tests are successful.
1 if some tests fail, or if there is an error.
"""
if not test_paths or len(test_paths) < 1:
return 1
print 'Found the following tests to run:'
for test in test_paths:
print '\t%s' % test
# Run all tests and remembers those that failed.
failed_tests = [t for t in test_paths if RunTest(t)]
print '\n\n%s test executables were run.' % len(test_paths)
failed_test_count = len(failed_tests)
if failed_test_count:
# Lists the executables that failed so the user can investigate them.
print 'FAILED!'
print 'The following %s tests failed:\n' % failed_test_count
for test in failed_tests:
print test
return 1
else:
# No, there is none.
if test_paths:
print 'All of them PASSED!'
return 0
def GetTestsInDirs(test_dirs):
"""Returns a list of all unit test executables in test_dirs.
Does not search subdirectories.
Args:
test_dirs: A list of directories to search.
Returns:
List of all unit tests.
"""
tests = []
for test_dir in test_dirs:
# Use dircache.listdir so order is alphabetical and thus deterministic.
files = dircache.listdir(test_dir)
for test_file in files:
if test_file.endswith(TEST_EXECUTABLE_RHS):
relative_path = os.path.join(test_dir, test_file)
if os.path.isfile(relative_path):
tests += [relative_path]
return tests
# Run a unit test when the module is run directly.
if __name__ == '__main__':
# List of paths that contain unit tests to run.
dirs_containing_tests = [STAGING_PATH, TESTS_PATH]
tests_to_run = GetTestsInDirs(dirs_containing_tests)
RunTests(tests_to_run)